Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
Minor enhancements to shaders & texture tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
allenhux-intel committed Jun 29, 2022
1 parent f9a352e commit 9bbec41
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 87 deletions.
73 changes: 17 additions & 56 deletions DdsToXet/DdsToXet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

//-----------------------------------------------------------------------------
Expand Down
15 changes: 6 additions & 9 deletions src/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)));
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
18 changes: 7 additions & 11 deletions src/shaders/terrainPS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions src/shaders/terrainVS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,7 +60,7 @@ struct VS_OUT
{
float4 pos : SV_POSITION;
float3 normal : NORMAL;
float3 eyeToPoint : EYETOPOINT;
float3 worldPos : WORLDPOS;
float2 tex : TEX;
};

Expand All @@ -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;
Expand Down

0 comments on commit 9bbec41

Please sign in to comment.