Skip to content

Commit

Permalink
shader fixes, and added new format conversions for texture saving
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Nov 10, 2021
1 parent d3c11ed commit ddb2248
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
2 changes: 1 addition & 1 deletion WickedEngine/shaders/chromatic_aberrationCS.hlsl
Expand Up @@ -5,7 +5,7 @@ PUSHCONSTANT(postprocess, PostProcess);

TEXTURE2D(input, float4, TEXSLOT_ONDEMAND0);

RWTEXTURE2D(output, unorm float4, 0);
RWTEXTURE2D(output, float4, 0);

[numthreads(POSTPROCESS_BLOCKSIZE, POSTPROCESS_BLOCKSIZE, 1)]
void main(uint3 DTid : SV_DispatchThreadID)
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/shaders/fsr_sharpenCS.hlsl
Expand Up @@ -11,7 +11,7 @@ static const uint4 Sample = 0;

TEXTURE2D(input, float4, TEXSLOT_ONDEMAND0);

RWTEXTURE2D(output, unorm float4, 0);
RWTEXTURE2D(output, float4, 0);

#define FSR_RCAS_F
AF4 FsrRcasLoadF(ASU2 p) { return input.Load(int3(ASU2(p), 0)); }
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/shaders/fsr_upscalingCS.hlsl
Expand Up @@ -11,7 +11,7 @@ static const uint4 Sample = 0;

TEXTURE2D(input, float4, TEXSLOT_ONDEMAND0);

RWTEXTURE2D(output, unorm float4, 0);
RWTEXTURE2D(output, float4, 0);

AF4 FsrEasuRF(AF2 p) { AF4 res = input.GatherRed(sampler_linear_clamp, p, int2(0, 0)); return res; }
AF4 FsrEasuGF(AF2 p) { AF4 res = input.GatherGreen(sampler_linear_clamp, p, int2(0, 0)); return res; }
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/shaders/fxaaCS.hlsl
Expand Up @@ -17,7 +17,7 @@ static const float fxaaEdgeThresholdMin = 0.0833;

TEXTURE2D(input, float4, TEXSLOT_ONDEMAND0);

RWTEXTURE2D(output, unorm float4, 0);
RWTEXTURE2D(output, float4, 0);

[numthreads(POSTPROCESS_BLOCKSIZE, POSTPROCESS_BLOCKSIZE, 1)]
void main(uint3 DTid : SV_DispatchThreadID)
Expand Down
4 changes: 2 additions & 2 deletions WickedEngine/shaders/sharpenCS.hlsl
Expand Up @@ -5,7 +5,7 @@ PUSHCONSTANT(postprocess, PostProcess);

TEXTURE2D(input, float4, TEXSLOT_ONDEMAND0);

RWTEXTURE2D(output, unorm float4, 0);
RWTEXTURE2D(output, float4, 0);

[numthreads(POSTPROCESS_BLOCKSIZE, POSTPROCESS_BLOCKSIZE, 1)]
void main(uint3 DTid : SV_DispatchThreadID)
Expand All @@ -16,5 +16,5 @@ void main(uint3 DTid : SV_DispatchThreadID)
float4 right = input[DTid.xy + int2(1, 0)];
float4 bottom = input[DTid.xy + int2(0, 1)];

output[DTid.xy] = saturate(center + (4 * center - top - bottom - left - right) * postprocess.params0[0]);
output[DTid.xy] = center + (4 * center - top - bottom - left - right) * postprocess.params0[0];
}
61 changes: 54 additions & 7 deletions WickedEngine/wiHelper.cpp
Expand Up @@ -199,8 +199,7 @@ namespace wiHelper

if (desc.Format == FORMAT_R10G10B10A2_UNORM)
{
// So this should be converted first to rgba8 before saving to common format...

// This will be converted first to rgba8 before saving to common format:
uint32_t* data32 = (uint32_t*)texturedata.data();

for (uint32_t i = 0; i < data_count; ++i)
Expand All @@ -222,14 +221,13 @@ namespace wiHelper
}
else if (desc.Format == FORMAT_R32G32B32A32_FLOAT)
{
// So this should be converted first to rgba8 before saving to common format...

XMFLOAT4* data128 = (XMFLOAT4*)texturedata.data();
// This will be converted first to rgba8 before saving to common format:
XMFLOAT4* dataSrc = (XMFLOAT4*)texturedata.data();
uint32_t* data32 = (uint32_t*)texturedata.data();

for (uint32_t i = 0; i < data_count; ++i)
{
XMFLOAT4 pixel = data128[i];
XMFLOAT4 pixel = dataSrc[i];
float r = std::max(0.0f, std::min(pixel.x, 1.0f));
float g = std::max(0.0f, std::min(pixel.y, 1.0f));
float b = std::max(0.0f, std::min(pixel.z, 1.0f));
Expand All @@ -244,6 +242,55 @@ namespace wiHelper
data32[i] = rgba8;
}
}
else if (desc.Format == FORMAT_R16G16B16A16_FLOAT)
{
// This will be converted first to rgba8 before saving to common format:
XMHALF4* dataSrc = (XMHALF4*)texturedata.data();
uint32_t* data32 = (uint32_t*)texturedata.data();

for (uint32_t i = 0; i < data_count; ++i)
{
XMHALF4 pixel = dataSrc[i];
float r = std::max(0.0f, std::min(XMConvertHalfToFloat(pixel.x), 1.0f));
float g = std::max(0.0f, std::min(XMConvertHalfToFloat(pixel.y), 1.0f));
float b = std::max(0.0f, std::min(XMConvertHalfToFloat(pixel.z), 1.0f));
float a = std::max(0.0f, std::min(XMConvertHalfToFloat(pixel.w), 1.0f));

uint32_t rgba8 = 0;
rgba8 |= (uint32_t)(r * 255.0f) << 0;
rgba8 |= (uint32_t)(g * 255.0f) << 8;
rgba8 |= (uint32_t)(b * 255.0f) << 16;
rgba8 |= (uint32_t)(a * 255.0f) << 24;

data32[i] = rgba8;
}
}
else if (desc.Format == FORMAT_R11G11B10_FLOAT)
{
// This will be converted first to rgba8 before saving to common format:
XMFLOAT3PK* dataSrc = (XMFLOAT3PK*)texturedata.data();
uint32_t* data32 = (uint32_t*)texturedata.data();

for (uint32_t i = 0; i < data_count; ++i)
{
XMFLOAT3PK pixel = dataSrc[i];
XMVECTOR V = XMLoadFloat3PK(&pixel);
XMFLOAT3 pixel3;
XMStoreFloat3(&pixel3, V);
float r = std::max(0.0f, std::min(pixel3.x, 1.0f));
float g = std::max(0.0f, std::min(pixel3.y, 1.0f));
float b = std::max(0.0f, std::min(pixel3.z, 1.0f));
float a = 1;

uint32_t rgba8 = 0;
rgba8 |= (uint32_t)(r * 255.0f) << 0;
rgba8 |= (uint32_t)(g * 255.0f) << 8;
rgba8 |= (uint32_t)(b * 255.0f) << 16;
rgba8 |= (uint32_t)(a * 255.0f) << 24;

data32[i] = rgba8;
}
}
else if (IsFormatBlockCompressed(desc.Format))
{
basisu::texture_format fmt;
Expand Down Expand Up @@ -278,7 +325,7 @@ namespace wiHelper
}
else
{
assert(desc.Format == FORMAT_R8G8B8A8_UNORM); // If you need to save other backbuffer format, convert the data here yourself...
assert(desc.Format == FORMAT_R8G8B8A8_UNORM); // If you need to save other texture format, implement data conversion for it
}

if (basis || ktx2)
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiVersion.cpp
Expand Up @@ -9,7 +9,7 @@ namespace wiVersion
// minor features, major updates, breaking compatibility changes
const int minor = 58;
// minor bug fixes, alterations, refactors, updates
const int revision = 17;
const int revision = 18;

const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);

Expand Down

0 comments on commit ddb2248

Please sign in to comment.