Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rlgl] rlReadShaderBufferElements() for compute shader does not work #2911

Closed
ffwqo opened this issue Feb 5, 2023 · 13 comments
Closed

[rlgl] rlReadShaderBufferElements() for compute shader does not work #2911

ffwqo opened this issue Feb 5, 2023 · 13 comments

Comments

@ffwqo
Copy link

ffwqo commented Feb 5, 2023

For some reason rlReadShaderBufferElements or rlReadShaderBuffer doesn't work the dest array remains unchanged. I have tested the compute shader via a normal opengl program and it works as expected. I have tried the master branch version of raylib but there I get the error symbol lookup error: ./build/main: undefined symbol: rlReadShaderBuffer at runtime. The compute GoL example in example/others also doesn't work.

#include <iostream>
#include <vector>
#include <raylib.h>
#include <assert.h>
#define GRAPHICS_API_OPENGL_43
#include <rlgl.h>

const char* shaderSource = 
R"(
#version 430
layout(local_size_x = 1) in;
layout(std430, binding=1) buffer shaderBuffer {
  float[] data;
};
void main() {
  uint idx= gl_GlobalInvocationID.x;
  data[idx] = 2.0 * idx;
}
)";
//void rlReadShaderBuffer(unsigned int id, void *dest, unsigned int count, unsigned int offset)
//{
//#if defined(GRAPHICS_API_OPENGL_43)
//    glBindBuffer(GL_SHADER_STORAGE_BUFFER, id);
//    glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, count, dest);
//#endif
//}

int main() {

    InitWindow(100, 100, "some");
    unsigned int shaderID = rlCompileShader(shaderSource, RL_COMPUTE_SHADER);
    unsigned int shaderProgram = rlLoadComputeShaderProgram(shaderID);

    std::vector<float> data{1,2,3,4,5,6,7,8,9,10};
    std::vector<float> target{0,2,4,6,8,10,12,14,16,18};
    unsigned int ssboA = rlLoadShaderBuffer(data.size() * sizeof(float), data.data(), RL_DYNAMIC_DRAW);

    rlEnableShader(shaderProgram);
    rlBindShaderBuffer(ssboA, 1);
    rlComputeShaderDispatch(data.size(), 1, 1);


    while(!WindowShouldClose()) {
      ClearBackground(BLACK);
      BeginDrawing();
      DrawRectangle(0, 0, 50, 50, GREEN);
      EndDrawing();
    }
    puts("##unloading##");
    rlReadShaderBufferElements(ssboA, data.data(), data.size() * sizeof(float), 0);
    for(int i=0; i < data.size(); ++i) assert(data[i] == target[i]); #fails

    rlUnloadShaderProgram(shaderProgram);
    rlUnloadShaderBuffer(ssboA);
    CloseWindow();
}
INFO: Initializing raylib 4.2
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 2016 x 1138
INFO:     > Screen size:  100 x 100
INFO:     > Render size:  100 x 100
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 233
INFO: GL: OpenGL device information:
INFO:     > Vendor:   AMD
INFO:     > Renderer: AMD Radeon Graphics (renoir, LLVM 15.0.7, DRM 3.49, 6.1.8-1-default)
INFO:     > Version:  4.6 (Core Profile) Mesa 22.3.4
INFO:     > GLSL:     4.60

using linux.

@sDos280
Copy link
Contributor

sDos280 commented Feb 5, 2023

@ffwqo, firstly note that rlUpdateShaderBufferElement is the "old name" of the rlUpdateShaderBuffer function...

secondly, how did you compile raylib? Did you make sure that you compile it for OpenGL4.3? Are you sure that your GPU supports OpenGL 4.3?

@raysan5
Copy link
Owner

raysan5 commented Feb 5, 2023

@ffwqo You have to recompile raylib with GRAPHICS_API_OPENGL_43 flag.

@sDos280
Copy link
Contributor

sDos280 commented Feb 5, 2023

note: I think that this layout(local_size_x = 1) in; is an allowed syntax in glsl... I think it needs to be layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;.

@ffwqo
Copy link
Author

ffwqo commented Feb 5, 2023

using glGetString(GL_VERSION) I get 4.6 (Core Profile) Mesa 22.3.4 so I think the gpu is supported
I have been using make PLATFORM=PLATFORM_DESKTOP GRAPHICS=GRAPHICS_API_OPENGL_43 RAYLIB_LIBTYPE=SHARED USE_EXTERNAL_GLFW=TRUE -j8 to compile raylib is that wrong? Maybe I should just use rlgl.h as a header only library...

@sDos280
Copy link
Contributor

sDos280 commented Feb 5, 2023

using glGetString(GL_VERSION) I get 4.6 (Core Profile) Mesa 22.3.4 so I think the gpu is supported.

still not sure that it really means that your GPU supports OpenGL 4.3 but ok.

is that wrong?

that seems fine to me.

may you send the complete console log? it seems that the current log that you sent is missing the shader compilation ouput stuff...

@ffwqo
Copy link
Author

ffwqo commented Feb 5, 2023

Going through the disscussion forum it seems I am not the only one with the issue @FranciscoJintoFox in #2734

INFO: Initializing raylib 4.2
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 2016 x 1138
INFO:     > Screen size:  100 x 100
INFO:     > Render size:  100 x 100
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 233
INFO: GL: OpenGL device information:
INFO:     > Vendor:   AMD
INFO:     > Renderer: AMD Radeon Graphics (renoir, LLVM 15.0.7, DRM 3.49, 6.1.8-1-default)
INFO:     > Version:  4.6 (Core Profile) Mesa 22.3.4
INFO:     > GLSL:     4.60
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
##unloading##
main: main.cpp:53: int main(): Assertion `data[i] == target[i]' failed.
fish: Job 1, './main' terminated by signal SIGABRT (Abort)

@sDos280
Copy link
Contributor

sDos280 commented Feb 6, 2023

@ffwqo, just tested your code and it seems to work well for me.
that is the code that I tested:

#include <iostream>
#include <vector>
#include <raylib.h>
#include <cassert>
#include <rlgl.h>

const char* shaderSource =
        R"(
#version 430
layout(local_size_x = 1) in;
layout(std430, binding=1) buffer shaderBuffer {
  float[] data;
};
void main() {
  uint idx= gl_GlobalInvocationID.x;
  data[idx] = 2.0 * idx;
}
)";

int main() {

    InitWindow(100, 100, "some");
    unsigned int shaderID = rlCompileShader(shaderSource, RL_COMPUTE_SHADER);
    unsigned int shaderProgram = rlLoadComputeShaderProgram(shaderID);

    std::vector<float> data{1,2,3,4,5,6,7,8,9,10};
    std::vector<float> target{0,2,4,6,8,10,12,14,16,18};
    unsigned int ssboA = rlLoadShaderBuffer(data.size() * sizeof(float), data.data(), RL_DYNAMIC_DRAW);

    rlEnableShader(shaderProgram);
    rlBindShaderBuffer(ssboA, 1);
    rlComputeShaderDispatch(data.size(), 1, 1);


    while(!WindowShouldClose()) {
        ClearBackground(BLACK);
        BeginDrawing();
        DrawRectangle(0, 0, 50, 50, GREEN);
        EndDrawing();
    }
    puts("##unloading##");
    rlReadShaderBuffer(ssboA, data.data(), data.size() * sizeof(float), 0);

    for(int i=0; i < data.size(); ++i) assert(data[i] == target[i]);

    rlUnloadShaderProgram(shaderProgram);
    rlUnloadShaderBuffer(ssboA);
    CloseWindow();
}

@sDos280
Copy link
Contributor

sDos280 commented Feb 6, 2023

so, as I see. it seems that there aren't any problems with raylib itself. check again if your GPU supports openGL4.3...
as you can see, every time you compile a compute shader using raylib you should get a log like this:
INFO: SHADER: [ID _ID_NUMBER_] Compute shader program loaded successfully
but it seems that you got none, the only things that can cause that (in your case) are 1. you didn't compile raylib for opengl4.3 support 2. your GPU doesn't support openGL4.3...

@raysan5
Copy link
Owner

raysan5 commented Feb 6, 2023

@ffwqo did you test the compute shader example provided ib examples/others/rlgl_compute_shader.c? I just tested it and it works as expected for me, here the output log:

INFO: Initializing raylib 4.5-dev
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 1920 x 1080
INFO:     > Screen size:  768 x 768
INFO:     > Render size:  768 x 768
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 241
INFO: GL: OpenGL device information:
INFO:     > Vendor:   Intel
INFO:     > Renderer: Intel(R) Iris(R) Xe Graphics
INFO:     > Version:  4.3.0 - Build 30.0.100.9864
INFO:     > GLSL:     4.30 - Build 30.0.100.9864
INFO: GL: VAO extension detected, VAO functions loaded successfully
INFO: GL: NPOT textures extension detected, full NPOT textures supported
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: GL: Compute shaders supported
INFO: GL: Shader storage buffer objects supported
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
INFO: FILEIO: [resources/shaders/glsl430/gol.glsl] Text file loaded successfully
INFO: SHADER: [ID 4] Compute shader compiled successfully
INFO: SHADER: [ID 5] Compute shader program loaded successfully
INFO: FILEIO: [resources/shaders/glsl430/gol_render.glsl] Text file loaded successfully
INFO: SHADER: [ID 6] Fragment shader compiled successfully
INFO: SHADER: [ID 7] Program shader loaded successfully
INFO: SHADER: [ID 7] Shader attribute (vertexPosition) set at location: 0
INFO: SHADER: [ID 7] Shader attribute (vertexTexCoord) set at location: 1
WARNING: SHADER: [ID 7] Failed to find shader attribute: vertexTexCoord2
WARNING: SHADER: [ID 7] Failed to find shader attribute: vertexNormal
WARNING: SHADER: [ID 7] Failed to find shader attribute: vertexTangent
WARNING: SHADER: [ID 7] Failed to find shader attribute: vertexColor
INFO: SHADER: [ID 7] Shader uniform (mvp) set at location: 0
WARNING: SHADER: [ID 7] Failed to find shader uniform: matView
WARNING: SHADER: [ID 7] Failed to find shader uniform: matProjection
WARNING: SHADER: [ID 7] Failed to find shader uniform: matModel
WARNING: SHADER: [ID 7] Failed to find shader uniform: matNormal
WARNING: SHADER: [ID 7] Failed to find shader uniform: colDiffuse
WARNING: SHADER: [ID 7] Failed to find shader uniform: texture0
WARNING: SHADER: [ID 7] Failed to find shader uniform: texture1
WARNING: SHADER: [ID 7] Failed to find shader uniform: texture2
INFO: SHADER: [ID 7] Shader uniform (resolution) set at location: 1
INFO: FILEIO: [resources/shaders/glsl430/gol_transfert.glsl] Text file loaded successfully
INFO: SHADER: [ID 6] Compute shader compiled successfully
INFO: SHADER: [ID 8] Compute shader program loaded successfully
INFO: TEXTURE: [ID 3] Texture loaded successfully (768x768 | R8G8B8A8 | 1 mipmaps)
INFO: SHADER: [ID 8] Unloaded shader program data from VRAM (GPU)
INFO: SHADER: [ID 5] Unloaded shader program data from VRAM (GPU)
INFO: TEXTURE: [ID 3] Unloaded texture data from VRAM (GPU)
INFO: SHADER: [ID 7] Unloaded shader program data from VRAM (GPU)
INFO: TEXTURE: [ID 2] Unloaded texture data from VRAM (GPU)
INFO: SHADER: [ID 3] Default shader unloaded successfully
INFO: TEXTURE: [ID 1] Default texture unloaded successfully
INFO: Window closed successfully

I also recommend you to use latest raylib from GitHub master branch, that's the upcoming raylib 4.5-dev version.

@raysan5 raysan5 changed the title Compute shader rlReadShaderBufferElements does not work [rlgl] rlReadShaderBufferElements() for compute shader does not work Feb 6, 2023
@ffwqo
Copy link
Author

ffwqo commented Feb 7, 2023

I have compiled the rlgl_computer_shader example from master branch with mkdir build && cd build && cmake -DCUSTOMIZE_BUILD=ON -DBUILD_SHARED_LIBS=ON -DPLATFORM=Desktop -DOPENGL_VERSION=4.3 .. && make -j8 and the compute shader part is missing. Am I missing some compiler flags when compiling raylib?

INFO: Initializing raylib 4.5-dev
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 2016 x 1138
INFO:     > Screen size:  768 x 768
INFO:     > Render size:  768 x 768
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 233
INFO: GL: OpenGL device information:
INFO:     > Vendor:   AMD
INFO:     > Renderer: AMD Radeon Graphics (renoir, LLVM 15.0.7, DRM 3.49, 6.1.8-1-default)
INFO:     > Version:  4.6 (Core Profile) Mesa 22.3.4
INFO:     > GLSL:     4.60
INFO: GL: VAO extension detected, VAO functions loaded successfully
INFO: GL: NPOT textures extension detected, full NPOT textures supported
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
INFO: FILEIO: [resources/shaders/glsl430/gol.glsl] Text file loaded successfully
INFO: FILEIO: [resources/shaders/glsl430/gol_render.glsl] Text file loaded successfully
INFO: SHADER: [ID 5] Fragment shader compiled successfully
INFO: SHADER: [ID 6] Program shader loaded successfully
INFO: SHADER: [ID 6] Shader attribute (vertexPosition) set at location: 0
INFO: SHADER: [ID 6] Shader attribute (vertexTexCoord) set at location: 1
WARNING: SHADER: [ID 6] Failed to find shader attribute: vertexTexCoord2
WARNING: SHADER: [ID 6] Failed to find shader attribute: vertexNormal
WARNING: SHADER: [ID 6] Failed to find shader attribute: vertexTangent
WARNING: SHADER: [ID 6] Failed to find shader attribute: vertexColor
INFO: SHADER: [ID 6] Shader uniform (mvp) set at location: 0
WARNING: SHADER: [ID 6] Failed to find shader uniform: matView
WARNING: SHADER: [ID 6] Failed to find shader uniform: matProjection
WARNING: SHADER: [ID 6] Failed to find shader uniform: matModel
WARNING: SHADER: [ID 6] Failed to find shader uniform: matNormal
WARNING: SHADER: [ID 6] Failed to find shader uniform: colDiffuse
WARNING: SHADER: [ID 6] Failed to find shader uniform: texture0
WARNING: SHADER: [ID 6] Failed to find shader uniform: texture1
WARNING: SHADER: [ID 6] Failed to find shader uniform: texture2
INFO: SHADER: [ID 6] Shader uniform (resolution) set at location: 1
INFO: FILEIO: [resources/shaders/glsl430/gol_transfert.glsl] Text file loaded successfully
INFO: TEXTURE: [ID 3] Texture loaded successfully (768x768 | R8G8B8A8 | 1 mipmaps)
INFO: SHADER: [ID 0] Unloaded shader program data from VRAM (GPU)
INFO: SHADER: [ID 0] Unloaded shader program data from VRAM (GPU)
INFO: TEXTURE: [ID 3] Unloaded texture data from VRAM (GPU)
INFO: SHADER: [ID 6] Unloaded shader program data from VRAM (GPU)
INFO: TEXTURE: [ID 2] Unloaded texture data from VRAM (GPU)
INFO: SHADER: [ID 3] Default shader unloaded successfully
INFO: TEXTURE: [ID 1] Default texture unloaded successfully
INFO: Window closed successfully

@raysan5
Copy link
Owner

raysan5 commented Feb 7, 2023

@ffwqo I'm afraid you are not compiling or linking raylib for OpenGL 4.3. You should review building. I don't use CMake, only the plain Makefiles and it works for me.

@raysan5
Copy link
Owner

raysan5 commented Feb 9, 2023

It seems this issue could be related to user build configuration not raylib so I'm closing the issue. Feel free to reopen it if further investigation provides more details.

@raysan5 raysan5 closed this as completed Feb 9, 2023
@ckw
Copy link

ckw commented Jun 1, 2023

I ran into a similar issue. The problem was not successfully defining GRAPHICS_API_OPENGL_43. I still don't know how to do that correctly, despite trying many variations on make PLATFORM=PLATFORM_DESKTOP GRAPHICS=GRAPHICS_API_OPENGL_43 RAYLIB_LIBTYPE=SHARED.

What 'worked' was adding set(GRAPHICS GRAPHICS_API_OPENGL_43) to the top of CMakeLists.txt. Or adding

#ifndef GRAPHICS_API_OPENGL_43
  #define GRAPHICS_API_OPENGL_43
#endif

to the top of rlgl.h

I put 'worked' in quotes because the game of life example works after doing this, but there are warnings during compilation like:

In file included from /home/ckw/raylib/src/rcore.c:121:                     
/home/ckw/raylib/src/rlgl.h: In function ‘rlGetShaderBufferSize’:                                                                                                  
/home/ckw/raylib/src/rlgl.h:4132:52: warning: passing argument 2 of ‘glad_glGetInteger64v’ from incompatible pointer type [-Wincompatible-pointer-types]           
 4132 |     glGetInteger64v(GL_SHADER_STORAGE_BUFFER_SIZE, &size);          
      |                                                    ^~~~~                 
      |                                                    |                                                                                                       
      |                                                    long long int *   
/home/ckw/raylib/src/rlgl.h:4132:52: note: expected ‘GLint64 *’ {aka ‘long int *’} but argument is of type ‘long long int *’ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants