Write to UTexture* from PixelShader

Hi all,

I’ve followed the excelent tutorial by Temaran @ Pixel and Compute shader in UE4, and I want to make some modifications and came across some errors, and need some help. I cannot seem to find any advanced details inside the official docs.

Background:

  1. Current example code in tutorial shows how to create dynamicTexture, assign it to StaticMesh, then pass it on to compute shader (that in turn modify it), and later on passed on to PixelShader for it to draw.
  2. Current example use a “1st-person shooter” template, that upon OnFire() event, perform ray tracing to hit object, and that chosen as “render target”.

My Goals:

  1. Make plugin a type of “Actor component” that I can attach to certain objects.
    Then the “effect” will auto run (upon “Play”) on the attached material.
  2. Make the pixel shader write to the texture itself
    (In original example compute shader did the texture modify & pixel shader just read & wrote as is…)
    Now I want to modify from within the Pixel Shader.

Whats done so far:

  1. I’ve created ActorComponent plugin, made code that wraps both Vertex Shader & Pixel Shader (similar to demo project provided).
  2. Upon BeginPlay() event i’m doing GetOwner()->GetComponents,
  3. Then itearted over, to extract the Material from it, and then extracted the texture from it as well (UTexture).
  4. Upon Tick() event I enqueue call to ExecutePixelShader with function that perform parameters assign & draw of screen-aligned quad.

Where I’m stuck ?
Whenever I pass the texture I want to draw on, when I reach the code line:

CurrentTexture = CurrentRenderTarget->GetRenderTargetResource()->GetRenderTargetTexture();

I get exception thrown, saying “read access violation”, as if the texture I’m using is “read only” in some way.

My questions are:

  1. Can someone please “clear” for me from all texture types (FTexture2DRHIRef, UTexture*, UTextureRenderTarget2d*, FShaderResourceViewRHIRef) - what do I need to pass down the shader, in order to be able to read-write from/to it ?
  2. Do I need to assign staticMesheCompoenent a Dynamic Texture ?
  3. I’m kinda “lost” with current example in demo, regarding the textures, why does the “FPixelShaderUsageExample” has 3 textures (“CurrentTexture”, “TextureParameter”, “CurrentRenderTarget”) and on top of it a “TextureParameterSRV” ?
    Suppose I want just to pass pointer to the texture I want to write onto, does it still require all of the above ?

would deeply appreciate if anyone can help on matter.

… did you get this done? .,… I`m looking for a sample C++ of shaders… the example above is not working at all on 4.19 …