OpenGL + SDL2 +计算着色器=黑屏

问题描述 投票:1回答:1

我正在将C raytracer转换为glsl,以便可以在计算着色器中使用它。现在,我只是试图在屏幕上渲染一些模糊的图案。没有编译错误,但是没有渲染。

我正在关注这两个教程(后者相当不完整,遗漏了一些部分,我可能在那里错过了一些东西)SDL2 with OpenGL - Lee Zhi EngCompute Shaders - Anton's OpenGL4 Tutorials

我不是opengl的专家,所以我不确定着色器是否输出到纹理?还是没有绘制纹理?还是其他问题?

[请发布确切的语法,而不仅仅是像“您需要通过初始化y和刷新z来完成x”之类的模糊指令,因为我可能不知道执行xyz的语法。

请注意:主程序仍在C中,而不是C ++。

编译为:-std=gnu18 -ffast-math -O3 -lm -lGLEW -lGL -lglut -Wall -pedantic -Werror -Wshadow -Wstrict-aliasing -Wstrict-overflow

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glu.h>
#include <GL/gl.h>

#include <SDL2/SDL.h>

int main(void)
{
    int tex_w = 1920;
    int tex_h = 1080;



    //////////////////////
    //
    //   SDL Stuff

    if (SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    SDL_Window * window;
    window = SDL_CreateWindow(
        "Rendering first frame", 
        SDL_WINDOWPOS_UNDEFINED, 
        SDL_WINDOWPOS_UNDEFINED, 
        tex_w*scale, 
        tex_h*scale, 
        SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
    );

    if (window == NULL){
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    #ifdef FULLSCREEN
    SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
    #endif

    SDL_Event event;



    //////////////////////
    //
    //   GL Stuff

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    SDL_GLContext glContext = SDL_GL_CreateContext(window);

    if (glContext == NULL){
        printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    glewInit();

    GLuint tex_output;
    glGenTextures(1, &tex_output);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, tex_output);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, tex_w, tex_h, 0, GL_RGBA, GL_FLOAT, NULL);
    glBindImageTexture(0, tex_output, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);



    //////////////////////
    //
    //   init compute shader

        const GLchar *the_ray_shader_string = "#ifdef GL_ES\
precision mediump float;\
#endif\
\
#extension GL_OES_standard_derivatives : enable\
\
uniform float time;\
uniform vec2 mouse;\
uniform vec2 resolution;\
layout(local_size_x = 1, local_size_y = 1) in;\
layout(rgba32f, binding = 0) uniform image2D img_output;\
\
void main( void ) {\
\
    vec2 position = ( gl_FragCoord.xy / resolution.xy ) + mouse / 4.0;\
\
    float color = 0.0;\
    color += sin( position.x * cos( time / 15.0 ) * 80.0 ) + cos( position.y * cos( time / 15.0 ) * 10.0 );\
    color += sin( position.y * sin( time / 10.0 ) * 40.0 ) + cos( position.x * sin( time / 25.0 ) * 40.0 );\
    color += sin( position.x * sin( time / 5.0 ) * 10.0 ) + sin( position.y * sin( time / 35.0 ) * 80.0 );\
    color *= sin( time / 10.0 ) * 0.5;\
\
    gl_FragColor = vec4( vec3( color, color * 0.5, sin( color + time / 3.0 ) * 0.75 ), 1.0 );\
\
}\0";

    GLuint ray_shader = glCreateShader(GL_COMPUTE_SHADER);
    glShaderSource(ray_shader, 1, &the_ray_shader_string, NULL);
    glCompileShader(ray_shader);

    GLuint ray_program = glCreateProgram();
    glAttachShader(ray_program, ray_shader);
    glLinkProgram(ray_program);


    int delta, leftToWait;

    int startTime, i = 0;

    while (++i && demoRunning)
    {
        startTime = SDL_GetTicks();

        SDL_PollEvent(&event);
        demoHandleInput(&event);

        glClear(GL_COLOR_BUFFER_BIT);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, tex_output);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        glUseProgram(ray_program);
        glDispatchCompute((GLuint)tex_w, (GLuint)tex_h, 1);

        glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

        SDL_GL_SwapWindow(window);

        delta = SDL_GetTicks() - startTime;
        leftToWait = (16 - delta); // aim for 60 fps

        if (leftToWait > 0){
            SDL_Delay(leftToWait);
        }
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCES;
}
c opengl glsl sdl compute-shader
1个回答
1
投票

您所谓的计算着色器实际上是Fragment Shader而不是Compute Shadergl_FragCoord是片段着色器的内置输入,gl_FragColor是(不建议使用的)片段着色器输出变量。附加到GL_COMPUTE_SHADER着色器对象,此着色器将无法编译。

您必须使用gl_GlobalInvocationID识别调用,并且必须使用gl_GlobalInvocationID将纹理像素写入输出图像(imageStore)。例如:

imageStore

但是请注意,这不会在窗口(默认帧缓冲区)上绘制任何内容,计算机着色器的目标是图像。计算着色器仅设置图像中的像素。如果要“看到”显示器上的图像,则必须渲染一个带有纹理对象的四边形,或将纹理附加到帧缓冲区,然后将其“ blit”(img_output)附加到默认帧缓冲区。

热门问题
推荐问题
最新问题