0

我正在开发一个放置系统,我从特定参数中找到计算着色器中的位置,然后将它们附加到 AppendStructuredBuffer。这按预期工作。问题源于在另一个着色器中使用生成的缓冲区,该着色器也尝试追加到新的 AppendStructuredBuffer。该缓冲区的输出比输入的要少得多。

最终将结果复制到最终缓冲区以使用 DrawMeshInstancedIndirect 进行渲染。

这是计算着色器,它只是将对象放置在平面上。

AppendStructuredBuffer<VegetationData> _OutVegetationData;
[numthreads(32,32,1)]
void VegetationPlacement (uint3 id : SV_DispatchThreadID)
{ 

VegetationData data;
float2 remappedVal;
Remap_float(float2(id.x, id.y), float2(0,512), float2(-256, 256), remappedVal);    
data.position = half4(float4(objPos, 0) + float4(float3(remappedVal.x, 0, remappedVal.y), 1));
data.normals = half3(0,1,0);
data.scale = 1;
_OutVegetationData.Append(data);
        
return;

而调度 - 在更新中运行,着色器已在启动函数中实例化。

void Update()
{
foreach (var vegetation in terrain.VegetationsOnTerrain)
            {
                if (vegetation.vegetationDataBuffer == null)
                {
                    vegetation.vegetationDataBuffer = new ComputeBuffer(512*512, 16 + 28, ComputeBufferType.Append);
                    vegetation.vegetationComputeShaderInstance.SetBuffer(vegetation.vegetationKernel, "_OutVegetationData", vegetation.vegetationDataBuffer);
                    vegetation.vegetationComputeShaderInstance.SetVector("objPos", terrain.transform.position);
                    vegetation.vegetationDataBuffer?.SetCounterValue(0);  
           vegetation.vegetationComputeShaderInstance.Dispatch(vegetation.vegetationKernel,
                    (int)512/32,
                    (int) 512 / 32,
                    (int) 1);
                }
            }

它应该看起来如何

然后我有另一个计算着色器,它使用该缓冲区作为 StructuredBuffer 来读取,然后填充另一个 AppendStrucuredBuffer,最终会在这里发生一些剔除。这是第二个计算着色器:

StructuredBuffer<VegetationData> _InVerticeData;
AppendStructuredBuffer<VegetationData> _OutVegetationData;
[numthreads(32,32,1)]
void LevelOfDetail (uint3 id : SV_DispatchThreadID)
{
_OutVegetationData.Append(_InVegetationData[id.x*id.y]);
return;
}

这是我调用第二个计算着色器的方法。它在第一个着色器调度之后运行。

第一个着色器的 AppendStructuredBuffer 在第二个计算着色器中设置为 StructuredBuffer“_InVegetationData”。

foreach (var vegetation in terrain.VegetationsOnTerrain)
            {
                if (vegetation.vegetationLodDataBuffer == null)
                {
                    vegetation.lodComputeShaderInstance.SetBuffer(vegetation.vegetationLodKernel, "_InVegetationData", vegetation.vegetationDataBuffer);
                    
                    vegetation.vegetationLodDataBuffer = new ComputeBuffer(512 * 512, 16 + 28, ComputeBufferType.Append);
                    vegetation.lodComputeShaderInstance.SetBuffer(vegetation.vegetationLodKernel, "_OutVegetationData", vegetation.vegetationLodDataBuffer);
                    vegetation.materialInstance.SetBuffer("vegetationDataBuffer", vegetation.vegetationLodDataBuffer);                 
                    }   
            
                    vegetation.vegetationLodDataBuffer?.SetCounterValue(0);
                  vegetation.lodComputeShaderInstance.Dispatch(vegetation.vegetationLodKernel,
                    (int)512/32,
                    (int)512/32,
                    (int) 1 / 1);
                
                ComputeBuffer.CopyCount(vegetation.vegetationLodDataBuffer, vegetation.argsBuffer, 4);

            }

使用第二个计算着色器后的样子:注意细化。

结果的希望是第一个计算着色器/缓冲区的输出,但如果我不将它们附加到第二个计算着色器中,它们就不会出现。我认为这可能/肯定是 GPU 未完成或类似的问题,但我不太确定。任何帮助都感激不尽!

谢谢。

4

0 回答 0