From 9bbec419645616216ed75a261a4d58255b42c8c0 Mon Sep 17 00:00:00 2001 From: allen hux Date: Wed, 29 Jun 2022 16:24:04 -0700 Subject: [PATCH] Minor enhancements to shaders & texture tool. --- DdsToXet/DdsToXet.cpp | 73 +++++++++----------------------------- src/Scene.cpp | 15 ++++---- src/Scene.h | 9 +++-- src/shaders/terrainPS.hlsl | 18 ++++------ src/shaders/terrainVS.hlsl | 8 ++--- 5 files changed, 36 insertions(+), 87 deletions(-) diff --git a/DdsToXet/DdsToXet.cpp b/DdsToXet/DdsToXet.cpp index 319b14b..4458c35 100644 --- a/DdsToXet/DdsToXet.cpp +++ b/DdsToXet/DdsToXet.cpp @@ -84,65 +84,26 @@ void Error(std::wstring in_s) //----------------------------------------------------------------------------- static DXGI_FORMAT GetFormatFromHeader(const DirectX::DDS_HEADER& in_ddsHeader) { - const auto& ddpf = in_ddsHeader.ddspf; - if (ddpf.flags & DDS_FOURCC) - { - if (MAKEFOURCC('D', 'X', 'T', '1') == ddpf.fourCC) - { - return DXGI_FORMAT_BC1_UNORM; - } - if (MAKEFOURCC('D', 'X', 'T', '3') == ddpf.fourCC) - { - return DXGI_FORMAT_BC2_UNORM; - } - if (MAKEFOURCC('D', 'X', 'T', '5') == ddpf.fourCC) - { - return DXGI_FORMAT_BC3_UNORM; - } - - // While pre-multiplied alpha isn't directly supported by the DXGI formats, - // they are basically the same as these BC formats so they can be mapped - if (MAKEFOURCC('D', 'X', 'T', '2') == ddpf.fourCC) - { - return DXGI_FORMAT_BC2_UNORM; - } - if (MAKEFOURCC('D', 'X', 'T', '4') == ddpf.fourCC) - { - return DXGI_FORMAT_BC3_UNORM; - } - - if (MAKEFOURCC('A', 'T', 'I', '1') == ddpf.fourCC) - { - return DXGI_FORMAT_BC4_UNORM; - } - if (MAKEFOURCC('B', 'C', '4', 'U') == ddpf.fourCC) - { - return DXGI_FORMAT_BC4_UNORM; - } - if (MAKEFOURCC('B', 'C', '4', 'S') == ddpf.fourCC) - { - return DXGI_FORMAT_BC4_SNORM; - } - - if (MAKEFOURCC('A', 'T', 'I', '2') == ddpf.fourCC) - { - return DXGI_FORMAT_BC5_UNORM; - } - if (MAKEFOURCC('B', 'C', '5', 'U') == ddpf.fourCC) - { - return DXGI_FORMAT_BC5_UNORM; - } - if (MAKEFOURCC('B', 'C', '5', 'S') == ddpf.fourCC) - { - return DXGI_FORMAT_BC5_SNORM; - } + DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN; - // other formats? + if (in_ddsHeader.ddspf.flags & DDS_FOURCC) + { + UINT32 fourCC = in_ddsHeader.ddspf.fourCC; + if (DirectX::DDSPF_DXT1.fourCC == fourCC) { format = DXGI_FORMAT_BC1_UNORM; } + if (DirectX::DDSPF_DXT2.fourCC == fourCC) { format = DXGI_FORMAT_BC2_UNORM; } + if (DirectX::DDSPF_DXT3.fourCC == fourCC) { format = DXGI_FORMAT_BC2_UNORM; } + if (DirectX::DDSPF_DXT4.fourCC == fourCC) { format = DXGI_FORMAT_BC3_UNORM; } + if (DirectX::DDSPF_DXT5.fourCC == fourCC) { format = DXGI_FORMAT_BC3_UNORM; } + if (MAKEFOURCC('A', 'T', 'I', '1') == fourCC) { format = DXGI_FORMAT_BC4_UNORM; } + if (MAKEFOURCC('A', 'T', 'I', '2') == fourCC) { format = DXGI_FORMAT_BC5_UNORM; } + if (MAKEFOURCC('B', 'C', '4', 'U') == fourCC) { format = DXGI_FORMAT_BC4_UNORM; } + if (MAKEFOURCC('B', 'C', '4', 'S') == fourCC) { format = DXGI_FORMAT_BC4_SNORM; } + if (MAKEFOURCC('B', 'C', '5', 'U') == fourCC) { format = DXGI_FORMAT_BC5_UNORM; } + if (MAKEFOURCC('B', 'C', '5', 'S') == fourCC) { format = DXGI_FORMAT_BC5_SNORM; } } + if (DXGI_FORMAT_UNKNOWN == format) { Error(L"Texture Format Unknown"); } - Error(L"Texture Format Unknown"); - - return DXGI_FORMAT_UNKNOWN; + return format; } //----------------------------------------------------------------------------- diff --git a/src/Scene.cpp b/src/Scene.cpp index 18d5f62..b15ad55 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -266,6 +266,7 @@ void Scene::MoveView(int in_x, int in_y, int in_z) XMMATRIX translation = XMMatrixTranslation(x, y, z); m_viewMatrix = XMMatrixMultiply(m_viewMatrix, translation); + m_viewMatrixInverse = XMMatrixInverse(nullptr, m_viewMatrix); } //----------------------------------------------------------------------------- @@ -996,8 +997,8 @@ void Scene::CreateConstantBuffers() m_pFrameConstantData->g_lightDir = XMFLOAT4(-0.538732767f, 0.787301660f, 0.299871892f, 0); XMStoreFloat4(&m_pFrameConstantData->g_lightDir, XMVector4Normalize(XMLoadFloat4(&m_pFrameConstantData->g_lightDir))); - m_pFrameConstantData->g_lightColor = XMFLOAT4(1, 1, 1, 40.0f); - m_pFrameConstantData->g_specColor = XMFLOAT4(1, 1, 1, 1); + m_pFrameConstantData->g_lightColor = XMFLOAT4(1, 1, 1, 1); + m_pFrameConstantData->g_specularColor = XMFLOAT4(1, 1, 1, 18.f); D3D12_CONSTANT_BUFFER_VIEW_DESC constantBufferView = {}; constantBufferView.SizeInBytes = bufferSize; @@ -1434,20 +1435,16 @@ void Scene::StartScene() m_commandList->RSSetScissorRects(1, &m_scissorRect); SetSampler(); - m_pFrameConstantData->g_view = m_viewMatrix; - DirectX::XMVECTOR vEyePt = m_viewMatrixInverse.r[3]; - DirectX::XMStoreFloat4(&m_pFrameConstantData->g_eyePos, vEyePt); + DirectX::XMStoreFloat4(&m_pFrameConstantData->g_eyePos, m_viewMatrixInverse.r[3]); m_pFrameConstantData->g_visualizeFeedback = m_args.m_visualizeMinMip; if (m_args.m_lightFromView) { - auto transposeView = DirectX::XMMatrixTranspose(m_viewMatrix); - DirectX::XMVECTOR lookDir = DirectX::XMVectorNegate(transposeView.r[2]); - m_pFrameConstantData->g_lightDir = (XMFLOAT4&)lookDir; + XMStoreFloat4(&m_pFrameConstantData->g_lightDir, m_viewMatrixInverse.r[2]); } else { - m_pFrameConstantData->g_lightDir = XMFLOAT4(-0.449135751f, 0.656364977f, 0.25f, 0); + m_pFrameConstantData->g_lightDir = XMFLOAT4(-0.538732767f, -0.787301660f, -0.299871892f, 0); XMStoreFloat4(&m_pFrameConstantData->g_lightDir, XMVector4Normalize(XMLoadFloat4(&m_pFrameConstantData->g_lightDir))); } } diff --git a/src/Scene.h b/src/Scene.h index d6b8a70..c9a5af5 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -151,11 +151,10 @@ class Scene struct FrameConstantData { - DirectX::XMMATRIX g_view; - DirectX::XMFLOAT4 g_eyePos; - DirectX::XMFLOAT4 g_lightDir; - DirectX::XMFLOAT4 g_lightColor; // RGB + specular intensity - DirectX::XMFLOAT4 g_specColor; + DirectX::XMFLOAT4 g_eyePos; // eye world position + DirectX::XMFLOAT4 g_lightDir; // world space light direction + DirectX::XMFLOAT4 g_lightColor; // RGB + DirectX::XMFLOAT4 g_specularColor; // RGB + specular intensity int g_visualizeFeedback; }; diff --git a/src/shaders/terrainPS.hlsl b/src/shaders/terrainPS.hlsl index f22149a..9c12913 100644 --- a/src/shaders/terrainPS.hlsl +++ b/src/shaders/terrainPS.hlsl @@ -35,17 +35,16 @@ SamplerState g_sampler : register(s0); //------------------------------------------------------------------------- //------------------------------------------------------------------------- -float3 evaluateLight(in float3 normal, in float3 eyeToPoint) +float3 evaluateLight(in float3 normal, in float3 pos) { - float3 reflected = reflect(eyeToPoint, normal); - - // directional light - float3 pointToLight = g_lightDir.xyz; - + // directional light. from the point toward the light is the opposite direction. + float3 pointToLight = -g_lightDir.xyz; float diffuse = saturate(dot(pointToLight, normal)); + float3 eyeToPoint = normalize(pos - g_eyePos.xyz); + float3 reflected = reflect(eyeToPoint, normal); float specDot = saturate(dot(reflected, pointToLight)); - float specular = pow(specDot, 2 * g_lightColor.a); + float specular = pow(specDot, g_specularColor.a); float ambient = 0.1f; @@ -69,11 +68,8 @@ float4 ps(VS_OUT input) : SV_TARGET0 // clamp the streaming texture to the mip level specified in the min mip map float3 color = g_streamingTexture.Sample(g_sampler, input.tex, 0, mipLevel).rgb; + color *= evaluateLight(input.normal, input.worldPos); - float3 eyeToPoint = normalize(input.eyeToPoint); - color *= evaluateLight(input.normal, eyeToPoint); - - // returns 0xff if no associated min mip, that is, no texel was touched last frame if ((g_visualizeFeedback) && (mipLevel < 16)) { color = lerp(color, GetLodVisualizationColor(mipLevel), 0.3f); diff --git a/src/shaders/terrainVS.hlsl b/src/shaders/terrainVS.hlsl index 4a1ff21..28f7686 100644 --- a/src/shaders/terrainVS.hlsl +++ b/src/shaders/terrainVS.hlsl @@ -38,7 +38,6 @@ cbuffer ModelConstantData : register(b1) cbuffer FrameConstantData : register(b0) { - float4x4 g_view; float4 g_eyePos; float4 g_lightDir; float4 g_lightColor; // RGB + specular intensity @@ -61,7 +60,7 @@ struct VS_OUT { float4 pos : SV_POSITION; float3 normal : NORMAL; - float3 eyeToPoint : EYETOPOINT; + float3 worldPos : WORLDPOS; float2 tex : TEX; }; @@ -74,10 +73,7 @@ VS_OUT vs(VS_IN input) result.normal = normalize(mul((float3x3)g_worldTransform, input.normal)); // transform position into light coordinate frame (world) - float3 pos = mul(g_worldTransform, float4(input.pos, 1.0f)).xyz; - - // direction from eye to pos - result.eyeToPoint = normalize(pos - g_eyePos.xyz); + result.worldPos = mul(g_worldTransform, float4(input.pos, 1.0f)).xyz; result.tex = input.tex; return result;