AskingQuestions / Shadeup

A language for WebGPU that makes writing shaders easier
https://shadeup.dev
150 stars 4 forks source link

Pass Buffer<float4> into compute shader #8

Closed Zifkan closed 1 year ago

Zifkan commented 1 year ago

Hello im on UE 5.3. I need some help with passing data into like this. Params.CameraPlanes this is TArray. image

Declaration buffer look like this image

And in Compute shader image

So problem that data passed incorrect. Im not sure about using PF_FloatRGBA in GraphBuilder.CreateSRV. I also tried PF_A32B32G32R32F. So tips about this param will be welcome Thank you for any tips

AskingQuestions commented 1 year ago

Okay so 2 parts to this:

(1) You could use a static array to send over the planes assuming the number will be static. Here's a quick sample of code from something I did a little while ago (borrowed from internal engine code):

// Struct def
SHADER_PARAMETER_ARRAY(FVector4f, FrustumPlanes, [5])

...

// PassParameter setup
for (int32 PlaneIndex = 0; PlaneIndex < 5; ++PlaneIndex)
{
   PassParameters->FrustumPlanes[PlaneIndex] = FVector4f(InViewDesc.Planes[PlaneIndex]);
}

(2) I'm not exactly sure what might be going wrong with your code sample above but here are a few observations:

  1. Make sure to explicitly set the size of your vectors. FVector4f as opposed to FVector4 (32bit vs 64bit)
    1. SHADER_PARAMETER_RDG_BUFFER_SRV(Buffer<float4>, ...) -> SHADER_PARAMETER_RDG_BUFFER_SRV(Buffer<FVector4f>, ...)
    2. sizeof(FVector) -> sizeof(FVector4f)
  2. I believe PF_FloatRGBA is using 16bit floats. PF_A32B32G32R32F should be the one for 32bit. (although the ordering may need to be reassembled on the shader side)

Hope this helps.

Zifkan commented 1 year ago

Ty for you answer.

First way worked fine, but 2nd still not. Anyway i used easiest first way for pass my planes into shader and... i have same problem with mine BoundBoxesBuffer its doesn't pass data into too and there i can't use it as static because its huge and obvious dynamic

kristiamo commented 1 year ago

First way worked fine, but 2nd still not.

To add onto the answer you already got: I had a similar frustrating issue before I remembered UE internally uses doubles (FVector4 = FVector4d, not FVector4f). I'm assuming Params.CameraPlanes.GetData() is pointing to a TArray<FVector4>. So you were taking an array of 4 doubles, and telling the shader to interpret 4floats.

Your 2 options would be to convert your input Params.CameraPlanes (dispatch params) to a TArray<FVector4f> first OR go back to FVector4 everywhere but change all the shader buffers to Buffer<double4>

Zifkan commented 1 year ago

First way worked fine, but 2nd still not.

To add onto the answer you already got: I had a similar frustrating issue before I remembered UE internally uses doubles (FVector4 = FVector4d, not FVector4f). I'm assuming Params.CameraPlanes.GetData() is pointing to a TArray<FVector4>. So you were taking an array of 4 doubles, and telling the shader to interpret 4floats.

Your 2 options would be to convert your input Params.CameraPlanes (dispatch params) to a TArray<FVector4f> first OR go back to FVector4 everywhere but change all the shader buffers to Buffer<double4>

ty for reply i already solve this problem