为什么我的计算着色器不做任何事情?

时间:2016-06-20 03:38:34

标签: c# opengl textures compute-shader

我在OpenGL 4.5(c#中的OpenGL.net,但代码与普通的OpenGL代码非常相似),我正在尝试使用计算着色器。这就是我到目前为止所拥有的:

// (Compile and link stuff, this part I know works)

// Use the compute shader
Gl.UseProgram(program);

// Create the texture the compute shader will output to
int textureWidth = 512, textureHeight = 512;
uint[] texturesMaking = new uint[1];
Gl.GenTextures(texturesMaking);
uint texture = texturesMaking[0];
Gl.ActiveTexture(Gl.TEXTURE0);
Gl.BindTexture(TextureTarget.Texture2d, texture);
Gl.TextureParameter(Gl.TEXTURE_2D, Gl.TEXTURE_MAG_FILTER, Gl.NEAREST);
Gl.TextureParameter(Gl.TEXTURE_2D, Gl.TEXTURE_MIN_FILTER, Gl.NEAREST);
Gl.TexImage2D(TextureTarget.Texture2d, 0, Gl.RGBA32F, textureWidth, textureHeight, 0, PixelFormat.Rgba, PixelType.Float, IntPtr.Zero);

// Get the shader output variable and set it
int location = Gl.GetUniformLocation(program, "destTex");
Gl.Uniform1(location, texture); 
Gl.BindImageTexture(0, texture, 0, false, 0, Gl.READ_WRITE, Gl.RGBA32F);

// Send off the compute shader to do work, with one group per pixel
//  (I know this is far less than optimal, it was just a debugging thing)
Gl.DispatchCompute((uint)textureWidth, (uint)textureHeight, 1);

// Wait for compute shader to finish
Gl.MemoryBarrier(Gl.SHADER_IMAGE_ACCESS_BARRIER_BIT);

// Looking at the texture, it is black

这是我的计算着色器:

#version 450
layout(local_size_x = 1, local_size_y = 1) in;
layout(rgba32f) uniform image2D destTex;

void main () {
  imageStore (destTex, ivec2(gl_GlobalInvocationID.xy), vec4(1.0, 0.0, 1.0, 1.0));
}

如果我理解这一点,在着色器运行后我的纹理应该是紫色/粉红色,而是它只是黑色,并打印出一些像素' colors表示所有值都为零。我做错了什么?

1 个答案:

答案 0 :(得分:3)

问题在于如何告诉着色器使用哪种纹理。由

设置的值
    <div class="form-control braintree-hosted-fields-invalid" id="hosted-fields-number">
        <iframe src="https://assets.braintreegateway.com/hosted-fields/2.15.5/hosted-fields-frame.html#5c7db060-9271-4eac-b4a2-0f405d743293" frameborder="0" allowtransparency="true" scrolling="no" type="number" name="braintree-hosted-field-number" id="braintree-hosted-field-number" style="border: none; width: 100%; height: 100%; float: left;">
        </iframe>
    <div style="clear: both;">

必须是图像纹理单元(int location = Gl.GetUniformLocation(program, "destTex"); Gl.Uniform1(location, texture); 命令的第一个参数),而不是纹理本身。由于您使用的是图像纹理单元0,因此您还必须将0传递给制服:

Gl.BindImageTexture