Shader behaves differently in every API
- Daodan
- Topic Author
Less
More
5 years 8 months ago - 5 years 8 months ago #1
by Daodan
Shader behaves differently in every API was created by Daodan
I just wrote a shader that uses tex2Dfetch() in order to give the game a pixelated look.
Now the issue is that this shader behaves differently in every API (D3D9, D3D11, OGL).
Any ideas why that is?
Now the issue is that this shader behaves differently in every API (D3D9, D3D11, OGL).
- D3D9: No issue
- D3D11: Image shrinks to the top left screen corner with increasing mip level (while it is noticeable that it gets pixelated and not only smaller)
- OGL: Values for the mip level greater than 0 equal to a black screen
Warning: Spoiler!
#include "ReShade.fxh"
#define PIXELATE_MIPLEVELS 8
#define PIXELATE_MIPLEVELS_UNIFORM_MAX 7
uniform int iUIMiplevel<
ui_type = "drag";
ui_label = "LOD";
ui_min = 0; ui_max = PIXELATE_MIPLEVELS_UNIFORM_MAX;
ui_step = 1;
> = 0;
texture2D texMipmap { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; MipLevels = PIXELATE_MIPLEVELS; Format = RGBA8; };
sampler2D samplerMipmap { Texture = texMipmap; };
float3 MipMapPS(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target {
return tex2D(ReShade::BackBuffer, texcoord).rgb;
}
float3 PixelatePS(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target {
return tex2Dfetch(samplerMipmap, int4((int2)floor(vpos.xy / (iUIMiplevel + 1)), 0, iUIMiplevel)).rgb;
}
technique Pixelate {
pass {
VertexShader = PostProcessVS;
PixelShader = MipMapPS;
RenderTarget0 = texMipmap;
}
pass {
VertexShader = PostProcessVS;
PixelShader = PixelatePS;
/* RenderTarget = BackBuffer */
}
}
Any ideas why that is?
Last edit: 5 years 8 months ago by Daodan.
Please Log in or Create an account to join the conversation.
- crosire
Less
More
5 years 8 months ago #2
by crosire
Replied by crosire on topic Shader behaves differently in every API
There is a logical error in your code. Every mipmaps is a power of two smaller than the previous one, not linearly so. To get the result you want the code should be:
Funny enough I made the same mistake in the D3D9 implementation of tex2Dfetch. Which is why it "worked", when it should not have. It's fixed now: github.com/crosire/reshade/commit/e1ae7c...282b6d7319d307eab464 .
The OpenGL implementation is hard to fix. It needs to flip the Y coordinate, but that's only trivial if the input coordinates are guaranteed to go all the way from 0 to texture height. Which is not the case with your shader. Which is why it fails. Can't think of a good way to fix this unfortunately.
return tex2Dfetch(samplerMipmap, int4(floor(vpos.xy / exp2(iUIMiplevel)), 0, iUIMiplevel)).rgb;
Funny enough I made the same mistake in the D3D9 implementation of tex2Dfetch. Which is why it "worked", when it should not have. It's fixed now: github.com/crosire/reshade/commit/e1ae7c...282b6d7319d307eab464 .
The OpenGL implementation is hard to fix. It needs to flip the Y coordinate, but that's only trivial if the input coordinates are guaranteed to go all the way from 0 to texture height. Which is not the case with your shader. Which is why it fails. Can't think of a good way to fix this unfortunately.
The following user(s) said Thank You: Daodan
Please Log in or Create an account to join the conversation.
- Daodan
- Topic Author
Less
More
5 years 8 months ago #3
by Daodan
Replied by Daodan on topic Shader behaves differently in every API
Ah, ok. I totally forgot that with the power of two. Thanks.
But at least a bug got fixed!
But at least a bug got fixed!
Please Log in or Create an account to join the conversation.