maierfelix / webgpu

WebGPU for Node [Deprecated, Unmaintained]
MIT License
244 stars 17 forks source link

include glsl in ray tracing shader error #9

Closed yyc-git closed 4 years ago

yyc-git commented 4 years ago

Hello @maierfelix ! my code is: rchit glsl:

#version 460
#extension GL_NV_ray_tracing : require
#extension GL_GOOGLE_include_directive : enable
#pragma shader_stage(closest)

#include "wavefront.glsl"

...

wavefront.glsl:

struct sceneDesc
{
  int  objId;
  int  txtOffset;
  mat4 transfo;
  mat4 transfoIT;
};

when run the code, it error:

error: '#include' : #error unexpected include directive for header name: wavefront.glsl

How to include glsl??? Thanks very much!!!

maierfelix commented 4 years ago

The included glsl compiler doesn't handle includes, it's up to the application. You have to recursively lookup the content of the included file and inline it. This is how I do it in my projects - see here.

Alternatively you can use the nvk-essentials package which contains a GLSL compiler and support for includes.

yyc-git commented 4 years ago

Thanks for your reply!!!

Yes, I can use "include" by import your "loadShaderFile"(created from your project). But it has a little bug: inliner.mjs

import fs from "fs";
//should add this line!
import path from "path";

...

So the complete code to support include is: index.mjs

//should move inliner.mjs to my project

import { loadShaderFile } from "./inliner.mjs";
...
  // rasterization shaders
  let vertexShaderModule = device.createShaderModule({
    code: loadShaderFile(`${baseShaderPath}/screen.vert`)
  });
  let fragmentShaderModule = device.createShaderModule({
    code: loadShaderFile(`${baseShaderPath}/screen.frag`)
  });

  // ray-tracing shaders
  let rayGenShaderModule = device.createShaderModule({
    code: loadShaderFile(`${baseShaderPath}/ray-generation.rgen`)
  });
  let rayCHitShaderModule = device.createShaderModule({
    code: loadShaderFile(`${baseShaderPath}/ray-closest-hit.rchit`)
  });
  let rayMissShaderModule = device.createShaderModule({
    code: loadShaderFile(`${baseShaderPath}/ray-miss.rmiss`)
  });

ray-closest-hit.rchit

#version 460
...
#extension GL_GOOGLE_include_directive : enable
#pragma shader_stage(closest)

#include "wavefront.glsl"

wavefront.glsl

//define the export code like:
struct Data1
{
  float objId;
};

int getData2(){
  return 1;
}