Hello,
I am interested in learning about compute shaders and using them in UE4, and so I’m looking at this example project I found here.
I have looked also at using compute shaders in Unity, so I know some basics already.
Now, in the example the compute shader writes to an RWTexture2D, which is bound to an FTexture2DRHIRef through an FUnorderedAccessViewRHIRef.
FRHIResourceCreateInfo CreateInfo;
FTexture2DRHIRef Texture = RHICreateTexture2D(SizeX, SizeY, PF_R32_UINT, 1, 1,
TexCreate_ShaderResource | TexCreate_UAV, CreateInfo);
FUnorderedAccessViewRHIRef TextureUAV = RHICreateUnorderedAccessView(Texture);
/* Set inputs/outputs and dispatch compute shader */
ComputeShader->SetSurfaces(RHICmdList, TextureUAV);
ComputeShader->SetUniformBuffers(RHICmdList, ConstantParameters, VariableParameters);
DispatchComputeShader(RHICmdList, *ComputeShader, n, n, 1);
ComputeShader->UnbindBuffers(RHICmdList);
The example then uses the FTexture2DRHIRef as input to a PixelShader, which renders to a UTextureRenderTarget2D, which is then assigned to the parameter of the dynamic material instance.
My problem:
I am interested in modifying an existing texture in a compute shader, without necessarily needing a pixel shader. So I would like to directly use the texture referenced by the FTexture2DRHIRef in a dynamic material instance. Without going through more shaders or having to take the texture through the CPU. But there is just so much to the RHI side of the engine, which I’m quite new to, I haven’t been able to figure this out.
(On a side note, in future I’d also like to modify existing meshes in compute shaders (not adding/removing verts, but changing their position and normals at least). But I fear this will be even harder to find out if/how that is possible, so I’m focusing on the texture problem for now )