Skip to content

Commit

Permalink
Added support for roundNearest() in Sfx.
Browse files Browse the repository at this point in the history
* round() in HLSL is roundEven() in GLSL.
* roundNearest() is Sfx/HLSL is round in GLSL.
  • Loading branch information
AndrewRichards-Code committed Jan 15, 2025
1 parent 885e413 commit fb66120
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
16 changes: 16 additions & 0 deletions DirectX11/Sfx/shader_platform.sl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@
#ifndef BOTTOM_UP_TEXTURE_COORDINATES
#define BOTTOM_UP_TEXTURE_COORDINATES 1
#endif

float roundNearest(float x)
{
float x_f = floor(x);
float x_c = ceil(x);
return x_c - x <= x - x_f ? x_c : x_f;
}
float1 roundNearest(float1 x) { return float1(roundNearest(x.x)); }
float2 roundNearest(float2 x) { return float2(roundNearest(x.x), roundNearest(x.y)); }
float3 roundNearest(float3 x) { return float3(roundNearest(x.x), roundNearest(x.y), roundNearest(x.z)); }
float4 roundNearest(float4 x) { return float4(roundNearest(x.x), roundNearest(x.y), roundNearest(x.z), roundNearest(x.w)); }

float4x4 roundNearest(float4x4 x) { return float4x4(roundNearest(x[0]), roundNearest(x[1]), roundNearest(x[2]), roundNearest(x[3])); }
float3x3 roundNearest(float3x3 x) { return float3x3(roundNearest(x[0]), roundNearest(x[1]), roundNearest(x[2])); }
float2x2 roundNearest(float2x2 x) { return float2x2(roundNearest(x[0]), roundNearest(x[1])); }
float1x1 roundNearest(float1x1 x) { return float1x1(roundNearest(x[0])); }
#endif


Expand Down
14 changes: 14 additions & 0 deletions DirectX12/Sfx/shader_platform.sl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
#define BOTTOM_UP_TEXTURE_COORDINATES 1
#endif

float roundNearest(float x)
{
float x_f = floor(x);
float x_c = ceil(x);
return x_c - x <= x - x_f ? x_c : x_f;
}
float2 roundNearest(float2 x) { return float2(roundNearest(x.x), roundNearest(x.y)); }
float3 roundNearest(float3 x) { return float3(roundNearest(x.x), roundNearest(x.y), roundNearest(x.z)); }
float4 roundNearest(float4 x) { return float4(roundNearest(x.x), roundNearest(x.y), roundNearest(x.z), roundNearest(x.w)); }

float4x4 roundNearest(float4x4 x) { return float4x4(roundNearest(x[0]), roundNearest(x[1]), roundNearest(x[2]), roundNearest(x[3])); }
float3x3 roundNearest(float3x3 x) { return float3x3(roundNearest(x[0]), roundNearest(x[1]), roundNearest(x[2])); }
float2x2 roundNearest(float2x2 x) { return float2x2(roundNearest(x[0]), roundNearest(x[1])); }

#endif

#endif
4 changes: 3 additions & 1 deletion OpenGL/Sfx/OpenGL.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@
"frac": "fract",
"ddx": "dFdx",
"ddy": "dFdy",
"static": ""
"static": "",
"round": "roundEven",
"roundNearest": "round"
},
"maxShaderModel": 5.0
}
4 changes: 3 additions & 1 deletion Vulkan/Sfx/Vulkan.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@
"._m30": "[3][0]",
"._m31": "[3][1]",
"._m32": "[3][2]",
"._m33": "[3][3]"
"._m33": "[3][3]",
"round": "roundEven",
"roundNearest": "round"
}
}
4 changes: 3 additions & 1 deletion Vulkan/Sfx/VulkanAndroid.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@
"flat": "",
"ddx": "dFdx",
"ddy": "dFdy",
"static": ""
"static": "",
"round": "roundEven",
"roundNearest": "round"
}
}

0 comments on commit fb66120

Please sign in to comment.