Opengl计算着色器在家工作,但在其他任何地方都没有

时间:2018-03-07 21:38:26

标签: c++ opengl glsl compute-shader

当在i6和gtx 980ti的计算机上在Visual Studio中运行我的程序时,它可以工作,我可以看到我的场景但是当我在学校用xeon和gtx 1080ti在计算机上运行该程序时,我只得到一个黑屏

在我的家用电脑上运行Nsight显示没有api错误,而在学校发送电话后有1282错误。

C ++文件

lightingShaderProgram.activateShaderProgram();

uniformLoc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, "invView_matrix");
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, glm::value_ptr(invView_matrix));

uniformLoc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, "view_matrix");
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, glm::value_ptr(view_matrix));


//CAM pos
uniformLoc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, "view_position");
glUniform3fv(uniformLoc, 1, glm::value_ptr(gameScene->mainCamera->transform.position));


glUniform1i(glGetUniformLocation(lightingShaderProgram.screenQuadShader, "gPosition"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gPosition);

glUniform1i(glGetUniformLocation(lightingShaderProgram.screenQuadShader, "gNormal"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, gNormal);

glUniform1i(glGetUniformLocation(lightingShaderProgram.screenQuadShader, "gAlbedo"), 2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, gAlbedo);

glUniform1i(glGetUniformLocation(lightingShaderProgram.screenQuadShader, "gAORoughMet"), 3);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, gAORoughMet);

glUniform1i(glGetUniformLocation(lightingShaderProgram.screenQuadShader, "gEmissive"), 4);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, gEmissive);

glUniform1i(glGetUniformLocation(lightingShaderProgram.screenQuadShader, "gSSAO"), 5);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, ssaoShaderProgram.ssaoColorBuffer);

glUniform1i(glGetUniformLocation(postEffectShaderProgram.gShaderProgram, "FragColor"), 6);
glBindImageTexture(6, lColor, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);

glUniform1i(glGetUniformLocation(postEffectShaderProgram.gShaderProgram, "FragHDRColor"), 7);
glBindImageTexture(7, lHDRColor, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
int index = 8;

//Lights
for (int i = 0; i < lightsToRender.size(); i++)
{
    std::cout << "lights" << lightsToRender.size() << std::endl;
    std::string lightUniform;

    //Type
    lightUniform = "lights[" + std::to_string(i) + "].lightType";
    GLuint loc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, lightUniform.c_str());
    glUniform1i(loc, GLint(lightsToRender.at(i)->lightComponent->lightType));

    //Position
    lightUniform = "lights[" + std::to_string(i) + "].Position";
    loc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, lightUniform.c_str());
    glUniform3fv(loc, 1, glm::value_ptr(lightsToRender.at(i)->transform.position));

    //Direction
    lightUniform = "lights[" + std::to_string(i) + "].DirectionVector";
    loc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, lightUniform.c_str());
    glUniform3fv(loc, 1, glm::value_ptr(lightsToRender.at(i)->transform.up));

    //Color
    lightUniform = "lights[" + std::to_string(i) + "].Color";
    loc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, lightUniform.c_str());
    glUniform3fv(loc, 1, glm::value_ptr(lightsToRender.at(i)->lightComponent->color));
    //std::cout << " Color" << std::endl << directionalLightsToRender.at(i)->lightComponent->color.x << directionalLightsToRender.at(i)->lightComponent->color.y << directionalLightsToRender.at(i)->lightComponent->color.z << std::endl;

    //Intensity
    lightUniform = "lights[" + std::to_string(i) + "].Intensity";
    loc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, lightUniform.c_str());
    glUniform1f(loc, lightsToRender.at(i)->lightComponent->intensity);

    //Range
    lightUniform = "lights[" + std::to_string(i) + "].Range";
    loc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, lightUniform.c_str());
    glUniform1f(loc, lightsToRender.at(i)->lightComponent->range);


    //Light space matrix
    lightUniform = "lights[" + std::to_string(i) + "].lightSpace_matrix";
    loc = glGetUniformLocation(lightingShaderProgram.screenQuadShader, lightUniform.c_str());
    glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(lightsToRender.at(i)->lightComponent->lightSpaceMatrix));

    //Shadow depthmap
    lightUniform = "lights[" + std::to_string(i) + "].shadowMap";
    glUniform1i(glGetUniformLocation(lightingShaderProgram.screenQuadShader, lightUniform.c_str()), index);
    glActiveTexture(GL_TEXTURE0+index);
    glBindTexture(GL_TEXTURE_2D, lightsToRender.at(i)->lightComponent->depthMap);

    index++;

}
glMemoryBarrier(GL_ALL_BARRIER_BITS);
GLint error = glGetError();
glDispatchCompute(display_w / 32, (display_h / 30), 1);
error = glGetError();
glMemoryBarrier(GL_ALL_BARRIER_BITS);

C ++缓冲区创建

    void RenderManager::createGBuffer()
{
    //framebufferobject
    glGenFramebuffers(1, &gbo);
    glBindFramebuffer(GL_FRAMEBUFFER, gbo);

    //g-buffer position
    glGenTextures(1, &gPosition);
    glBindTexture(GL_TEXTURE_2D, gPosition);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, display_w, display_h, 0, GL_RGB, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glBindTexture(GL_TEXTURE_2D, 0);
    //attach texture to current framebuffer
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gPosition, 0);

    //g-buffer normal
    glGenTextures(1, &gNormal);
    glBindTexture(GL_TEXTURE_2D, gNormal);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, display_w, display_h, 0, GL_RGB, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glBindTexture(GL_TEXTURE_2D, 0);
    //attach texture to current framebuffer
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gNormal, 0);

    //g-buffer albedo
    glGenTextures(1, &gAlbedo);
    glBindTexture(GL_TEXTURE_2D, gAlbedo);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, display_w, display_h, 0, GL_RGB, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glBindTexture(GL_TEXTURE_2D, 0);
    //attach texture to current framebuffer
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, gAlbedo, 0);

    //g-buffer specular + metallic + AO
    glGenTextures(1, &gAORoughMet);
    glBindTexture(GL_TEXTURE_2D, gAORoughMet);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, display_w, display_h, 0, GL_RGB, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glBindTexture(GL_TEXTURE_2D, 0);
    //attach texture to current framebuffer
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, gAORoughMet, 0);

    //Emmisive
    glGenTextures(1, &gEmissive);
    glBindTexture(GL_TEXTURE_2D, gEmissive);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, display_w, display_h, 0, GL_RGB, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glBindTexture(GL_TEXTURE_2D, 0);
    //attach texture to current framebuffer
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_2D, gEmissive, 0);


    glDrawBuffers(5, gAttachments);


    //renderbufferobject
    glGenRenderbuffers(1, &gRbo);
    glBindRenderbuffer(GL_RENDERBUFFER, gRbo);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, display_w, display_h);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

    //attach renderbufferobject to framebuffer
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gRbo);

    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    {
        std::cout << "ERROR::GBUFFER:: Framebuffer is not complete! " << glCheckFramebufferStatus(GL_FRAMEBUFFER) << std::endl;
        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
        {
            std::cout << "Attachment" << std::endl;

        }

    }
    else
    {
        std::cout << "SUCCESS::FRAMEBUFFER:: Framebuffer is complete!" << std::endl;
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}

void RenderManager::createLPBuffer()
{
    glGenFramebuffers(1, &LPfbo);
    glBindFramebuffer(GL_FRAMEBUFFER, LPfbo);

    //Color attachments
    glGenTextures(1, &lColor);
    glBindTexture(GL_TEXTURE_2D, lColor);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, display_w, display_h, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glBindTexture(GL_TEXTURE_2D, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, lColor, 0);

    glGenTextures(1, &lHDRColor);
    glBindTexture(GL_TEXTURE_2D, lHDRColor);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, display_w, display_h, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_2D, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, lHDRColor, 0);

    glGenTextures(1, &lProcessedColor);
    glBindTexture(GL_TEXTURE_2D, lProcessedColor);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, display_w, display_h, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_2D, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, lProcessedColor, 0);

    glDrawBuffers(3, pAttachments);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}

GLSL

#version 440
#define EPSILON 0.0001
const float PI = 3.14159265359;

layout(local_size_x = 32, local_size_y = 30) in; //gl_GlobalInvocationID.xy(z?) gl_LocalInvocationID.xy(z?)

layout(binding = 0) uniform sampler2D gPosition;
layout(binding = 1) uniform sampler2D gNormal;
layout(binding = 2) uniform sampler2D gAlbedo;
layout(binding = 3) uniform sampler2D gAORoughMet;
layout(binding = 4) uniform sampler2D gEmissive;
layout(binding = 5) uniform sampler2D gSSAO;

layout ( binding = 6, rgba32f) uniform image2D FragColor;
layout ( binding = 7, rgba32f) uniform image2D FragHDRColor;

uniform vec3 view_position;
uniform mat4 invView_matrix;
uniform mat4 view_matrix;

const int NR_LIGHTS = 32;
struct Light {
    int lightType;
    vec3 Position;
    vec3 DirectionVector;
    vec3 Color;
    float Intensity;
    float Range;
    mat4 lightSpace_matrix;
    sampler2D shadowMap;

};
uniform Light lights[NR_LIGHTS];




float distributionGGX(vec3 normalV, vec3 halfwayV, float roughness){

    //Based on observations by Disney and adopted by Epic Games the 
    //lighting looks more correct squaring the roughness in both 
    //the geometry and normal distribution function.
    float roughness2 = roughness*roughness*roughness*roughness;
    float normalVdotHalfwayV=max(dot(normalV, halfwayV), 0.0);
    float normalVdotHalfwayV2=normalVdotHalfwayV*normalVdotHalfwayV;

    float denominator = normalVdotHalfwayV2*(roughness2-1.0)+1.0;
    denominator= PI *denominator*denominator;

    return roughness2/denominator;
}

float geometrySchlickGGX(float normalVdotViewDirV, float roughness){

    float rough = roughness+1.0;
    float k = (rough*rough)/8.0;
    float denominator = normalVdotViewDirV*(1.0-k)+k;

    return normalVdotViewDirV/denominator;
}

float GeometrySmith(vec3 normalV, vec3 viewDirV, vec3 lightDirV, float k){

    float normalVdotViewDirV = max(dot(normalV, viewDirV), 0.0);
    float normalVdotLightDirV = max(dot(normalV, lightDirV), 0.0);
    float ggx1 = geometrySchlickGGX(normalVdotViewDirV, k);
    float ggx2 = geometrySchlickGGX(normalVdotLightDirV, k);

    return ggx1*ggx2;
}

vec3 fresnelSchlik(float cosTheta, vec3 fresnel0){
    return fresnel0+(1.0-fresnel0)*pow(1.0 -cosTheta, 5.0);
}

void main(){
    // retrieve data from G-buffer
    vec2 uvCoord = vec2(gl_GlobalInvocationID.xy)/vec2(1280,720);// screen = 1280x720
    vec3 FragPos = texture(gPosition, uvCoord).rgb;
    vec3 Normal = texture(gNormal, uvCoord).rgb;
    vec3 Albedo = texture(gAlbedo, uvCoord).rgb;
    vec3 AORoughMet = texture(gAORoughMet, uvCoord).rgb;
    vec3 Emissive = texture(gEmissive, uvCoord).rgb;
    float SSAO = texture(gSSAO, uvCoord).r;
    vec3 viewDir = normalize((view_matrix*vec4(view_position, 1)).xyz - FragPos);

    //Base reflectivity
    vec3 fresnel0 = vec3(0.04);
    fresnel0 = mix(fresnel0, Albedo, AORoughMet.b);

    vec3 totalSpectralRadiance = vec3(0.0);


    //Lights
    for (int i = 0; i < NR_LIGHTS; ++i)
    {
        vec3 lightDir;
        vec3 halfwayDir;
        vec3 radiance;
        float shadow=0;
        if(lights[i].lightType==0){

        }

        //Directional
        if(lights[i].lightType==1){
            lightDir = normalize(lights[i].DirectionVector);
            lightDir = (view_matrix*vec4(lightDir, 0)).xyz;

            halfwayDir = normalize(lightDir + viewDir);

            //Shadow Calculations
            vec4 fragPosLightSpace = lights[i].lightSpace_matrix*invView_matrix*vec4(FragPos, 1.0);
            vec3 projectedCoordninates = fragPosLightSpace.xyz / fragPosLightSpace.w;
            projectedCoordninates = projectedCoordninates*0.5 + 0.5;
            float currentDepth = projectedCoordninates.z;
            vec2 texelSize = 1.0 / textureSize(lights[0].shadowMap, 0);
            if (currentDepth < 1.0) {
                float bias = max(0.05 * (dot(Normal, lightDir)), 0.05);

                for (int x = -1; x <= 1; ++x)
                {
                    for (int y = -1; y <= 1; ++y)
                    {
                        float pcfDepth = texture(lights[0].shadowMap, projectedCoordninates.xy + vec2(x, y) * texelSize).r;
                        shadow += currentDepth-bias > pcfDepth ? 1.0 : 0.0;
                    }
                }
            }
            shadow = shadow / 9.0;
            //End Shadow Calculations

            radiance = lights[i].Color*lights[i].Intensity;
        }

        //Point
        if(lights[i].lightType==2){
            lightDir = normalize((view_matrix*vec4( lights[i].Position, 1)).xyz  - FragPos);
            halfwayDir = normalize(lightDir + viewDir);  
            //
            //Shadow
            //
            float distance = length((view_matrix*vec4( lights[i].Position, 1)).xyz - FragPos);
            float attenuation = 1.0 / (distance*distance); 
            radiance = lights[i].Color*lights[i].Intensity*attenuation;

        }




        //Fresnel
        vec3 fresnel = fresnelSchlik(clamp(dot(halfwayDir, viewDir), 0.0, 1.0), fresnel0);

        //Normal distribution function: approximates the amount the surface's microfacets are aligned to the halfway vector influenced by the roughness of the surface
        float normalDistribution = distributionGGX(Normal, halfwayDir, AORoughMet.g);

        //Geometry function: describes the self-shadowing property of the microfacets.
        float geometryValue = GeometrySmith(Normal, viewDir, lightDir, AORoughMet.g);

        //Cook-Torrance bidirectional reflectance distribution function 
        vec3 nominator = normalDistribution*geometryValue*fresnel;
        float denominator = 4.0 * max(dot(Normal, viewDir), 0.0)*max(dot(Normal, lightDir), 0.0);
        vec3 specular = nominator / max(denominator, 0.001);

        vec3 contributionSpec = fresnel;
        vec3 contributionDif = vec3(1.0) - contributionSpec;
        contributionDif *= 1.0 - AORoughMet.b;

        float normalVdotLightDirV = max(dot(Normal, lightDir), 0.0);
        totalSpectralRadiance += (contributionDif*Albedo / PI + specular)*radiance*normalVdotLightDirV*(1-shadow);
    }
    float ao = AORoughMet.r;
    if (ao < SSAO) {
        ao = SSAO;
    }


    vec3 ambient = vec3(0.03)*Albedo;
    vec3 color= ambient + totalSpectralRadiance*ao;

    //HDR Output
    vec3 HDRValue = vec3(0, 0, 0);
    float brightness = dot(color.rgb, vec3(0.2126, 0.7152, 0.0722));
    if (brightness > 1.0) {
        HDRValue += color;
    }
    HDRValue += Emissive;
    //FragHDRColor = vec4(HDRValue, 1.0);
    imageStore(FragHDRColor, ivec2(gl_GlobalInvocationID.xy), vec4(HDRValue.xyz,1.0));

    vec3 outputColor = vec3(0, 0, 0);
    brightness = dot(color.rgb, vec3(1.0,1.0,1.0));
    if (brightness > EPSILON) {
        outputColor += color;
    }
    //FragColor = vec4(outputColor.xyz, 1.0);
    imageStore(FragColor, ivec2(gl_GlobalInvocationID.xy), vec4(outputColor.xyz,1.0));

    //FragColor = vec4(ao,ao,ao, 1.0);

}

//编辑 - 着色器创建

GLuint ShaderProgram::CreateShaderData(std::string vertexShader, std::string tesselationControlShader, std::string tesselationEvaluationShader, std::string geometryShader, std::string fragmentShader, std::string computeShader)
{

    // local buffer to store error strings when compiling.
    char buff[1024];
    memset(buff, 0, 1024);
    GLint compileResult = 0;
    ifstream shaderFile;
    std::string shaderText;
    const char* shaderTextPtr;
    GLuint gShaderProgram = 0;

    GLuint vs = 0;
    if (vertexShader != "") {
        //create vertex shader
        vs = glCreateShader(GL_VERTEX_SHADER);
        shaderFile.open(vertexShader);
        shaderText.assign((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
        shaderFile.close();

        shaderTextPtr = shaderText.c_str();

        // ask GL to use this string as shader code source
        glShaderSource(vs, 1, &shaderTextPtr, nullptr);

        // try to compile this shader source.
        glCompileShader(vs);

        // check for compilation error
        glGetShaderiv(vs, GL_COMPILE_STATUS, &compileResult);
        if (compileResult == GL_FALSE) {
            // query information about the compilation (nothing if compilation went fine!)
            glGetShaderInfoLog(vs, 1024, nullptr, buff);
            // print to Visual Studio debug console output
            OutputDebugStringA(buff);
        }
    }

    GLuint tcs = 0;
    if (tesselationControlShader != "") {
        //tesselation control shader
        tcs = glCreateShader(GL_TESS_CONTROL_SHADER);
        shaderFile.open(tesselationControlShader);
        shaderText.assign((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
        shaderFile.close();
        shaderTextPtr = shaderText.c_str();
        glShaderSource(tcs, 1, &shaderTextPtr, nullptr);
        glCompileShader(tcs);
        // query information about the compilation (nothing if compilation went fine!)
        compileResult = GL_FALSE;
        glGetShaderiv(tcs, GL_COMPILE_STATUS, &compileResult);
        if (compileResult == GL_FALSE) {
            // query information about the compilation (nothing if compilation went fine!)
            memset(buff, 0, 1024);
            glGetShaderInfoLog(tcs, 1024, nullptr, buff);
            // print to Visual Studio debug console output
            OutputDebugStringA(buff);
        }
    }

    GLuint tes = 0;
    if (tesselationEvaluationShader != "") {
        //tesselation evaluation shader
        tes = glCreateShader(GL_TESS_EVALUATION_SHADER);
        shaderFile.open(tesselationEvaluationShader);
        shaderText.assign((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
        shaderFile.close();
        shaderTextPtr = shaderText.c_str();
        glShaderSource(tes, 1, &shaderTextPtr, nullptr);
        glCompileShader(tes);
        // query information about the compilation (nothing if compilation went fine!)
        compileResult = GL_FALSE;
        glGetShaderiv(tes, GL_COMPILE_STATUS, &compileResult);
        if (compileResult == GL_FALSE) {
            // query information about the compilation (nothing if compilation went fine!)
            memset(buff, 0, 1024);
            glGetShaderInfoLog(tes, 1024, nullptr, buff);
            // print to Visual Studio debug console output
            OutputDebugStringA(buff);
        }
    }

    GLuint gs = 0;
    if (geometryShader != "") {
        //geometry shader
        gs = glCreateShader(GL_GEOMETRY_SHADER);
        shaderFile.open(geometryShader);
        shaderText.assign((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
        shaderFile.close();
        shaderTextPtr = shaderText.c_str();
        glShaderSource(gs, 1, &shaderTextPtr, nullptr);
        glCompileShader(gs);
        // query information about the compilation (nothing if compilation went fine!)
        compileResult = GL_FALSE;
        glGetShaderiv(gs, GL_COMPILE_STATUS, &compileResult);
        if (compileResult == GL_FALSE) {
            // query information about the compilation (nothing if compilation went fine!)
            memset(buff, 0, 1024);
            glGetShaderInfoLog(gs, 1024, nullptr, buff);
            // print to Visual Studio debug console output
            OutputDebugStringA(buff);
        }
    }

    GLuint fs = 0;
    if (fragmentShader != "") {
        // fragment Shader
        fs = glCreateShader(GL_FRAGMENT_SHADER);
        shaderFile.open(fragmentShader);
        shaderText.assign((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
        shaderFile.close();
        shaderTextPtr = shaderText.c_str();
        glShaderSource(fs, 1, &shaderTextPtr, nullptr);
        glCompileShader(fs);
        // query information about the compilation (nothing if compilation went fine!)
        compileResult = GL_FALSE;
        glGetShaderiv(fs, GL_COMPILE_STATUS, &compileResult);
        if (compileResult == GL_FALSE) {
            // query information about the compilation (nothing if compilation went fine!)
            memset(buff, 0, 1024);
            glGetShaderInfoLog(fs, 1024, nullptr, buff);
            // print to Visual Studio debug console output
            OutputDebugStringA(buff);
        }
    }

    GLuint cs = 0;
    if (computeShader != "") {
        //compute shader
        cs = glCreateShader(GL_COMPUTE_SHADER);
        shaderFile.open(computeShader);
        shaderText.assign((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
        shaderFile.close();
        shaderTextPtr = shaderText.c_str();
        glShaderSource(cs, 1, &shaderTextPtr, nullptr);
        glCompileShader(cs);
        // query information about the compilation (nothing if compilation went fine!)
        compileResult = GL_FALSE;
        glGetShaderiv(cs, GL_COMPILE_STATUS, &compileResult);
        if (compileResult == GL_FALSE) {
            // query information about the compilation (nothing if compilation went fine!)
            memset(buff, 0, 1024);
            glGetShaderInfoLog(cs, 1024, nullptr, buff);
            // print to Visual Studio debug console output
            OutputDebugStringA(buff);
        }
    }

    //link shader program (connect vs and ps)
    gShaderProgram = glCreateProgram();
    if (fragmentShader != "") {
        glAttachShader(gShaderProgram, fs);
    }
    if (tesselationControlShader != "") {
        glAttachShader(gShaderProgram, tcs);
    }
    if (tesselationEvaluationShader != "") {
        glAttachShader(gShaderProgram, tes);
    }
    if (geometryShader != "") {
        glAttachShader(gShaderProgram, gs);
    }
    if (vertexShader != "") {
        glAttachShader(gShaderProgram, vs);
    }
    if (computeShader != "") {
        glAttachShader(gShaderProgram, cs);
    }
    glLinkProgram(gShaderProgram);

    // check once more, if the Vertex Shader and the Fragment Shader can be used
    // together
    compileResult = GL_FALSE;
    glGetProgramiv(gShaderProgram, GL_LINK_STATUS, &compileResult);
    if (compileResult == GL_FALSE) {
        // query information about the compilation (nothing if compilation went fine!)
        memset(buff, 0, 1024);
        glGetProgramInfoLog(gShaderProgram, 1024, nullptr, buff);
        // print to Visual Studio debug console output
        OutputDebugStringA(buff);
    }

    //detach shaders
    if (fragmentShader != "") {
        glDetachShader(gShaderProgram, fs);
    }
    if (tesselationControlShader != "") {
        glDetachShader(gShaderProgram, tcs);
    }
    if (tesselationEvaluationShader != "") {
        glDetachShader(gShaderProgram, tes);
    }
    if (geometryShader != "") {
        glDetachShader(gShaderProgram, gs);
    }
    if (vertexShader != "") {
        glDetachShader(gShaderProgram, vs);
    }
    if (computeShader != "") {
        glDetachShader(gShaderProgram, cs);
    }

    //delete shaders
    if (fragmentShader != "") {
        glDeleteShader(fs);
    }
    if (tesselationControlShader != "") {
        glDeleteShader(tcs);
    }
    if (tesselationEvaluationShader != "") {
        glDeleteShader(tes);
    }
    if (geometryShader != "") {
        glDeleteShader(gs);
    }
    if (vertexShader != "") {
        glDeleteShader(vs);
    }
    if (computeShader != "") {
        glDeleteShader(cs);
    }



    return gShaderProgram;
}

我认为sampler2d和/或image2d存在问题,因为我之前在另一个计算着色器中遇到了问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

解决了它。在nvidia nsight-&gt; capture frame-&gt; misc-&gt;计算着色器限制中,我发现计算着色器只支持32个统一纹理。我有5个sampler2d用于gbuffer和32个阴影贴图sampler2ds。当我将它改为6 + 26时,它可以工作。