어떻게 작동하는지 이해하기 위해 OpenGL에 간단한 컴퓨팅 쉐이더를 작성하여 원하는 결과를 얻을 수 없습니다.Compute Shader에 구조체 배열
내 계산 쉐이더에 출력 텍스처를 채색하는 구조체 배열 colourStruct를 전달하고 싶습니다.
내가 빨간색 이미지를 가지고 싶을 때 "wantedColor"내 컴퓨 트 셰이더 = 0, 2
파란색 녹색 이미지 "wantedColor"= 1,하지만 실제로 때 "wantedColor"빨간색 만이 = 1 또는 2 또는 3이고 검정색이 "wantedColor"> 2 일 때 ...
누군가가 아이디어를 가지고 있거나 컴퓨팅 쉐이더가 아이디어를 입력하지 못했다고 생각할 수 있습니다.
도움을 주셔서 감사합니다. 여기 내 코드의 흥미로운 부분이 있습니다.
내 컴퓨 트 쉐이더 :
#version 430 compatibility
layout(std430, binding=4) buffer Couleureuh
{
vec3 Coul[3]; // array of structures
};
layout(local_size_x = 1, local_size_y = 1) in;
layout(rgba32f, binding = 0) uniform image2D img_output;
void main() {
// base pixel colour for image
vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0);
// get index in global work group i.e x,y, position
ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
ivec2 dims = imageSize (img_output);
int colorWanted = 0;
pixel = vec4(Coul[colorWanted], 1.0);
// output to a secific pixel in the image
imageStore (img_output, pixel_coords, pixel);
}
계산 쉐이더와 SSBO 초기화 :
GLuint structBuffer;
glGenBuffers(1, &structBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, structBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, 3*sizeof(colorStruct), NULL, GL_STATIC_DRAW);
GLint bufMask = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT; // invalidate makes a ig difference when re-writting
colorStruct *coul;
coul = (colorStruct *) glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, 3*sizeof(colorStruct), bufMask);
coul[0].r = 1.0f;
coul[0].g = 0.0f;
coul[0].b = 0.0f;
coul[1].r = 0.0f;
coul[1].g = 1.0f;
coul[1].b = 0.0f;
coul[2].r = 0.0f;
coul[2].g = 0.0f;
coul[2].b = 1.0f;
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, structBuffer);
m_out_texture.bindImage();
// Launch compute shader
m_shader.use();
glDispatchCompute(m_tex_w, m_tex_h, 1);
// Prevent samplign before all writes to image are done
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
플로트와 vec4를 사용 중입니다! 고마워요! :). – bRiocHe