计算着色器中的Image2D [英] Image2D in compute shader

查看:112
本文介绍了计算着色器中的Image2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将image2D用作顶点的2D存储,这些顶点将由计算着色器修改,但一切正常.

I want to use image2D as 2D storage for vertices which will be modified by compute shader but things doesnt work.

创建纹理:

glGenTextures(1, &HeightMap);
glBindTexture(GL_TEXTURE_2D, HeightMap);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA32F, 513, 513, 0,GL_RGBA32F, GL_UNSIGNED_BYTE, 0);

使用和调度计算着色器:

Use and dispatch compute shader:

glUseProgram(ComputeProgram);
glActiveTexture(GL_TEXTURE0);
glBindImageTexture(0, HeightMap, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
glDispatchCompute(1, 1, 1 );
glMemoryBarrier( GL_ALL_BARRIER_BITS );

然后计算着色器:

    #version 430 core
layout( std430, binding=1 ) buffer VertBuffer
    {
    vec4 Positions[ ]; 
    };
layout( local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout (binding=0, rgba32f)  uniform image2D HeightMap;
void main (void)
{
    imageStore(HeightMap, ivec2(0,0),vec4(0,0,0,1));
    Positions[0]=imageLoad(HeightMap, ivec2(0,0)).rgba;
}

推荐答案

好,我找到了解决方案.这是您使用image2D通过计算着色器读取和写入数据的方式:

Ok i found solution. This is how you use image2D for read and write data with compute shader:

创建纹理:

glGenTextures(1, &HeightMap);
glBindTexture(GL_TEXTURE_2D, HeightMap);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA32F, 513, 513, 0,GL_RGBA, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);

调度计算着色器:

glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 1, VerticesBuffer );

glBindImageTexture(0, HeightMap, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
glUseProgram(ComputeProgram);

glUniform1i(glGetUniformLocation(ComputeProgram, "HeightMap"), 0);


glDispatchCompute(1, 1, 1 );
glMemoryBarrier( GL_ALL_BARRIER_BITS );

计算着色器示例:

    #version 430 core
layout( std430, binding=1 ) buffer VertBuffer
    {
    vec4 Positions[ ]; 
    };
layout( local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout (rgba32f)  uniform image2D HeightMap;
void main (void)
{
    ivec2 pos=ivec2(0,0);
    imageStore(HeightMap, pos,vec4(10,0,0,1));
    Positions[0].xyzw=imageLoad(HeightMap, pos).rgba;
}

这篇关于计算着色器中的Image2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆