- Home /
Shader: Access current pixel screen position from within #ifdef SHADER_API_D3D11
Hello,
basically, what I want to do is access the current fragments screen coordinate. I know, I can do it like so uint xcoord = _ScreenParams.x * screenPos.x / screenPos.w;
and similar for the y coordinate. But this does not work if I call it from within a #ifdef ... #endif
block. I need the coordinate to get the right index for a StructuredBuffer
. This happens inside a function call from inside the surf function.
For debugging reasons, I used screenPos.xy/screenPos.w
as the red component of the object's color as I traced it down to this problem.
This works
float bub = screenPos.x/screenPos.w;
return bub;
but this does not
float bub = screenPos.x/screenPos.w;
#ifdef SHADER_API_D3D11
bub = screenPos.x/screenPos.w;
#endif
return bub;
The first one produces a nice red to black gradient (as expected) but the second one produces only black.
Oddly enough, this works too (gradient)
float bub = screenPos.x/screenPos.w;
#ifdef SHADER_API_D3D11
bub = screenPos.x/screenPos.w;
#else
bub = screenPos.x/screenPos.w;
#endif
return bub;
but this doesn't (black)
float bub = screenPos.x/screenPos.w;
#ifdef SHADER_API_D3D11
bub = screenPos.x/screenPos.w;
#else
bub = 1; //this does not get executed
#endif
return bub;
I also tried to render the x component only and the w component only. From within the block, it gives me a nearly completely red object and black (or nearly black) object respectively. From outside it is a gradient or at least reddish color for both.
I can't wrap my head around this behaviour. It appears strange to me.
Calculating the index outside also does not work. Using a hardcoded index does, though.
Your answer
Follow this Question
Related Questions
Use contents of RWStructuredBuffer written by shader in another shader 0 Answers
Adding a clip() to the default shader? 0 Answers
cast shadow - export lightmap from 3ds max to unity 2 Answers
Culling part of a sprite in Sprite Manager 2 1 Answer
Depth mask with alpha / adding alpha to frag grabpass shader 0 Answers