Quantcast
Channel: Questions in topic: "compute shader"
Viewing all articles
Browse latest Browse all 287

Why is this if statement failing on a Compute Shader?

$
0
0
I am trying to use a compute shader to change the colour of an image, and if I add no conditions this works fine. However, if I want to add an if statement to only change the pixels of a particular colour to this new colour, no pixels are changed at all. #pragma kernel CSMain struct ColorStruct { float4 col; }; RWTexture2D Result; RWStructuredBuffer data; [numthreads(8,8,1)] void CSMain (uint3 id : SV_DispatchThreadID) { // ColorStruct c = data[0]; // c.col = Result[id.xy]; // data[id.x] = c; if (all(Result[id.xy] == float4(0.937f, 0.110f, 0.145f, 1.000f))) { Result[id.xy] = float4(0, 0, 0, 1); } // Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0); } I have verified through the RWStructuredBuffer (see the 3 commented lines) that the colour is indeed (0.937f, 0.110f, 0.145f, 1.000f) as this is outputted to me, but for some reason it isn't picking up this colour within the if statement.
This is the code I use to call the shader: public struct ColorStruct { public Color col; } private ColorStruct[] data = new ColorStruct[2]; // Start is called before the first frame update void Start() { tex = new RenderTexture(map.width / 2, map.height / 2, 0); ComputeBuffer buf = new ComputeBuffer(map.width * map.height, 4); buf.SetData(data); tex.enableRandomWrite = true; Graphics.Blit(map, tex); tex.Create(); shader.SetBuffer(0, "data", buf); shader.SetTexture(0, "Result", tex); shader.Dispatch(0, tex.width / 8, tex.height / 8, 1); buf.GetData(data); buf.Dispose(); } I can't for the life of me figure out why the if statement is being ignored when the colour values are identical to what the buffer outputs.

Viewing all articles
Browse latest Browse all 287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>