简体   繁体   English

我的计算着色器数组索引有什么问题?

[英]What is wrong with my compute shader array indexing?

I'm currently having a problem with my compute shader failing to properly get an element at a certain index of an input array.我目前的计算着色器存在问题,无法正确获取输入数组某个索引处的元素。

I've read the buffers manually using NVidia NSight and it seems to be input properly, the problem seems to be with indexing.我已经使用 NVidia NSight 手动读取了缓冲区,它似乎输入正确,问题似乎出在索引上。

It's supposed to be drawing voxels on a grid, take this case as an example (What is supposed to be drawn is highlighted in red while blue is what I am getting):它应该是在网格上绘制体素,以这种情况为例(应该绘制的内容以红色突出显示,而蓝色是我得到的内容):

渲染帧

And here is the SSBO buffer capture in NSight transposed:这是 NSight 转置中的 SSBO 缓冲区捕获:

SSBO

This is the compute shader I'm currently using:这是我目前使用的计算着色器:

#version 430
layout(local_size_x = 1, local_size_y = 1) in;
layout(rgba32f, binding = 0) uniform image2D img_output;

layout(std430) buffer;

layout(binding = 0) buffer Input0 {
    ivec2 mapSize;
};

layout(binding = 1) buffer Input1 {
    bool mapGrid[];
};

void main() {
  // base pixel colour for image
  vec4 pixel = vec4(1, 1, 1, 1);
  // get index in global work group i.e x,y position
  ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
  vec2 normalizedPixCoords = vec2(gl_GlobalInvocationID.xy) / gl_NumWorkGroups.xy;
  ivec2 voxel = ivec2(int(normalizedPixCoords.x * mapSize.x), int(normalizedPixCoords.y * mapSize.y));
  
  float distanceFromMiddle = length(normalizedPixCoords - vec2(0.5, 0.5));

  pixel = vec4(0, 0, mapGrid[voxel.x * mapSize.x + voxel.y], 1); // <--- Where I'm having the problem
  // I index the voxels the same exact way on the CPU code and it works fine

  // output to a specific pixel in the image
  //imageStore(img_output, pixel_coords, pixel * vec4(vignettecolor, 1) * imageLoad(img_output, pixel_coords));
  imageStore(img_output, pixel_coords, pixel);
}

NSight doc file: https://ufile.io/wmrcy1l4 NSight 文档文件: https ://ufile.io/wmrcy1l4

I was able to fix the problem by completely ditching SSBOs and using a texture buffer, turns out the problem was that OpenGL treated each value as a 4-byte value and stepped 4 bytes instead of one for each index.我能够通过完全放弃 SSBO 并使用纹理缓冲区来解决这个问题,结果证明问题是 OpenGL 将每个值视为 4 字节值,并为每个索引步进 4 个字节而不是一个字节。

Based on this post: Shader storage buffer object with bytes基于这篇文章: Shader storage buffer object with bytes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM