samdauwe / webgpu-native-examples

Collection of C-language examples that demonstrate basic rendering and computation in WebGPU native.
Apache License 2.0
373 stars 21 forks source link

[Help] shader文件读取异常 #2

Closed mxy-nj closed 2 years ago

mxy-nj commented 2 years ago

在定义shader文件后, 使用 FILE API来读取shader文件失败,求大神指教 1. vertex shader 定义

WGPUVertexState vertex_state_desc = wgpu_create_vertex_state( wgpu_context, &(wgpu_vertex_state_t){ .shader_desc = (wgpu_shader_desc_t){ // Vertex shader SPIR-V .file = "shaders/compute_boids/shader.vert.spv", }, ... });

2. shader 文件读取, 问题是, 读到的文件size 是0, 但是file handle也不为NULL

FILE* file = fopen(filename, "rb"); if (file == NULL) { printf("Unable to open file '%s'\n", filename); return; } fseek(file, 0, SEEK_END); result->size = ftell(file); fseek(file, 0, SEEK_SET); printf("shader_read_file %s, result->size %d\n",filename, result->size);

samdauwe commented 2 years ago

Hi,

1. Can you check if the file exists?

afbeelding

The working directory is changed to the 'assets' folder with this line.

There is also a helper file that can check if the file exists.

Maybe you can print the full path were the file is read.

2. Could you try running example that uses a shader in text fileformat like for instance (to check if it is not an issue of reading binary files):

$ ./wgpu_sample_launcher compute_particles_webgpu_logo

This example will load WGSL shader (shader in text fileformat ) instead of SPIR-V (shader in binary format).

3. Does any file reading work ? An example on how to read files is given in this code. Maybe this code works?

4. Do you have read permissions on the file ? On my machine the file permissions are as follows:

ls -al
totaal 36
drwxrwxr-x.  2 sdauwe sdauwe 4096  1 jan 14:09 .
drwxrwxr-x. 48 sdauwe sdauwe 4096  1 jan 14:09 ..
-rw-rw-r--.  1 sdauwe sdauwe 2404 21 jun  2021 boids.comp
-rw-rw-r--.  1 sdauwe sdauwe 5444 21 jun  2021 boids.comp.spv
-rw-rw-r--.  1 sdauwe sdauwe   97 21 jun  2021 shader.frag
-rw-rw-r--.  1 sdauwe sdauwe  336 21 jun  2021 shader.frag.spv
-rw-rw-r--.  1 sdauwe sdauwe  395 21 jun  2021 shader.vert
-rw-rw-r--.  1 sdauwe sdauwe 1684 21 jun  2021 shader.vert.spv

For instance, you can give public permissions to the file for testing.

Hope it helps debugging the issue.

Best regards, Sam

mxy-nj commented 2 years ago

@samdauwe, Many appreciation for your reply.

  1. these shader file exists , and i am running the sample on web side
  2. without full compilation, i only compiled some demo into .js + .wasm + .html, using emsdk, On Windows env.
  3. if using the binary shader array like below, shader can work well; However, with api fopen and fread for the filename, in webside, exception will be thrown
  uint32_t const compute_boids_frag_spirv[] = {
 0x07230203,0x00010000,0x000d000a,0x0000000c, ...
  1. Should i use url like "http://localhost:8080/web/shaders/compute_boids/shader.vert.spv", instead of "shaders/compute_boids/shader.vert.spv" ? . And if use http url for filename, some httprequest api will be needed , like libcurl. i am great confused.
    Looking forward to your answer.
samdauwe commented 2 years ago

@mxy-nj, thanks for quickly providing the test results, now I better understand the root cause of the issue and it indeed seems to be related to running the example(s) on web side. I never done this before.

For emsdk, you probably need to package your files as explained on this documentation page

A practical example can be found in this repo:

About 4. yes maybe it works by getting the files from a web url but I never tried this myself.

Best regards, Sam

mxy-nj commented 2 years ago

@samdauwe, thanks for your help. The emcc linkder flags , as you mentioned, " --preload-file=...", works well for file reading. Following your guide to read the documenation page, it made great benefit. Thanks very much!

samdauwe commented 2 years ago

@mxy-nj, happy to hear that the emcc linker flag makes it possible to read the files!

Are you able run the examples in a web browser ? It is still on my roadmap to build for Web with Emscripten / WebAssembly.

Best regards, Sam

mxy-nj commented 2 years ago

@samdauwe Yes, web browser works well.