Skip to content

Commit

Permalink
Update HLSL Shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsmagd committed Sep 6, 2024
1 parent 9725920 commit 2a52f9d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Ifnity/Ifnity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ set(GLAD_SOURCE ${PROJECT_SOURCE_DIR}/vendor/glad/src/glad.cpp)
set(PCH_SOURCES ${PROJECT_SOURCE_DIR}/src/pch.cpp)

# Add library
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${PCH_SOURCES} ${GLAD_SOURCE})
add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES} ${PCH_SOURCES} ${GLAD_SOURCE})

target_precompile_headers(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/src/pch.h)

Expand Down
45 changes: 41 additions & 4 deletions Ifnity/Ifnity/src/Platform/Windows/DeviceD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ bool DeviceD3D12::InitializeDeviceAndContext()
if (adapterIndex == 0)
IFNITY_LOG(LogCore, ERROR, "Cannot find any DXGI adapters in the system. D3D12");
else
IFNITY_LOG(LogCore, ERROR, "The specified DXGI adapter %d does not exist. D3D12", adapterIndex);
//IFNITY_LOG(LogCore, ERROR, "The specified DXGI adapter %d does not exist. D3D12", adapterIndex);

return false;
}
Expand Down Expand Up @@ -317,12 +317,13 @@ bool DeviceD3D12::InitializeDeviceAndContext()
CreateSwapChain();
//Crate RTV and DSV Descriptor Heaps
CreateRtvAndDsvDescriptorHeaps();
LoadAssetDemo();

OnResize();

CaptureD3D12DebugMessages();

LoadAssetDemo();


return true;
}
Expand Down Expand Up @@ -841,14 +842,49 @@ void DeviceD3D12::BuildShadersAndInputLayout()
{
HRESULT hr = S_OK;

m_VsByteCode = CompileShader(L"Shaders\\color.hlsl", nullptr, "VS", "vs_5_0");
m_PsByteCode = CompileShader(L"Shaders\\color.hlsl", nullptr, "PS", "ps_5_0");
m_VsByteCode = CompileShader(L"Shaders\\shaders.hlsl", nullptr, "VSMain", "vs_5_0");
m_PsByteCode = CompileShader(L"Shaders\\shaders.hlsl", nullptr, "PSMain", "ps_5_0");

m_InputLayout =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
};
}

void DeviceD3D12::BuildPipelineStage()
{
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc;
ZeroMemory(&psoDesc, sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC));
psoDesc.InputLayout = { m_InputLayout.data(), (UINT)m_InputLayout.size() };
psoDesc.pRootSignature = m_RootSignature.Get();
psoDesc.VS =
{
reinterpret_cast<BYTE*>(m_VsByteCode->GetBufferPointer()),
m_VsByteCode->GetBufferSize()
};
psoDesc.PS =
{
reinterpret_cast<BYTE*>(m_PsByteCode->GetBufferPointer()),
m_PsByteCode->GetBufferSize()
};
psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
psoDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
psoDesc.SampleMask = UINT_MAX;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.NumRenderTargets = 1;
psoDesc.RTVFormats[0] = m_BackBufferFormat;
psoDesc.DSVFormat = m_DepthStencilFormat;
psoDesc.SampleDesc.Count = m_MsaaState ? 4 : 1; // Activate MSSA 4X , by default is false.
psoDesc.SampleDesc.Quality = CheckMSAAQualitySupport(psoDesc.SampleDesc.Count,
m_BackBufferFormat) - 1; // Its importa to substract 1 because the quality level is 0 based.
ThrowIfFailed(m_Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&m_PipelineState)));





}


Expand All @@ -857,6 +893,7 @@ void DeviceD3D12::LoadAssetDemo()
{
BuildRootSignature();
BuildShadersAndInputLayout();
BuildPipelineStage();
}


Expand Down
4 changes: 3 additions & 1 deletion Ifnity/Ifnity/src/Platform/Windows/DeviceD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class DeviceD3D12 final : public GraphicsDeviceManager

std::vector<D3D12_INPUT_ELEMENT_DESC> m_InputLayout;


//Pipeline State Objects
ComPtr<ID3D12PipelineState> m_PipelineState = nullptr;
D3D_DRIVER_TYPE m_D3dDriverType = D3D_DRIVER_TYPE_HARDWARE;
DXGI_FORMAT m_BackBufferFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
DXGI_FORMAT m_DepthStencilFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
Expand Down Expand Up @@ -111,6 +112,7 @@ class DeviceD3D12 final : public GraphicsDeviceManager
void LoadAssetDemo();
void BuildRootSignature();
void BuildShadersAndInputLayout();
void BuildPipelineStage();



Expand Down
42 changes: 42 additions & 0 deletions Shaders/color.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//***************************************************************************************
// color.hlsl by Frank Luna (C) 2015 All Rights Reserved.
//
// Transforms and colors geometry.
//***************************************************************************************

cbuffer cbPerObject : register(b0)
{
float4x4 gWorldViewProj;
};

struct VertexIn
{
float3 PosL : POSITION;
float4 Color : COLOR;
};

struct VertexOut
{
float4 PosH : SV_POSITION;
float4 Color : COLOR;
};

VertexOut VS(VertexIn vin)
{
VertexOut vout;

// Transform to homogeneous clip space.
vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);

// Just pass vertex color into the pixel shader.
vout.Color = vin.Color;

return vout;
}

float4 PS(VertexOut pin) : SV_Target
{
return pin.Color;
}


Binary file added Shaders/color_ps.cso
Binary file not shown.
Binary file added Shaders/color_vs.cso
Binary file not shown.
31 changes: 31 additions & 0 deletions Shaders/shaders.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

struct PSInput
{
float4 position : SV_POSITION;
float4 color : COLOR;
};

PSInput VSMain(float4 position : POSITION, float4 color : COLOR)
{
PSInput result;

result.position = position;
result.color = color;

return result;
}

float4 PSMain(PSInput input) : SV_TARGET
{
return input.color;
}

0 comments on commit 2a52f9d

Please sign in to comment.