Skip to content

Commit a048f5d

Browse files
committed
Shader source code to string
1 parent 5d1c2cc commit a048f5d

File tree

7 files changed

+129
-13
lines changed

7 files changed

+129
-13
lines changed

HDR10Capture2019/HDR10Capture.vcxproj

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,47 @@
8080
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
8181
<FileType>Document</FileType>
8282
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
83-
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">copy %(Identity) "$(OutDir)" &gt; NUL</Command>
84-
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">copy %(Identity) "$(OutDir)" &gt; NUL</Command>
85-
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)\%(Identity)</Outputs>
86-
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)\%(Identity)</Outputs>
87-
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</TreatOutputAsContent>
88-
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</TreatOutputAsContent>
83+
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">type $(ProjectDir)shaderStrHeader1.inc &gt; $(ProjectDir)%(Filename).inc
84+
echo g_%(Filename) &gt;&gt; $(ProjectDir)%(Filename).inc
85+
type $(ProjectDir)shaderStrHeader2.inc &gt;&gt; $(ProjectDir)%(Filename).inc
86+
type %(FullPath) &gt;&gt; $(ProjectDir)%(Filename).inc
87+
type $(ProjectDir)shaderStrFooter.inc &gt;&gt; $(ProjectDir)%(Filename).inc</Command>
88+
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">type $(ProjectDir)shaderStrHeader1.inc &gt; $(ProjectDir)%(Filename).inc
89+
echo g_%(Filename) &gt;&gt; $(ProjectDir)%(Filename).inc
90+
type $(ProjectDir)shaderStrHeader2.inc &gt;&gt; $(ProjectDir)%(Filename).inc
91+
type %(FullPath) &gt;&gt; $(ProjectDir)%(Filename).inc
92+
type $(ProjectDir)shaderStrFooter.inc &gt;&gt; $(ProjectDir)%(Filename).inc</Command>
93+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).inc</Outputs>
94+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).inc</Outputs>
95+
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</TreatOutputAsContent>
96+
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</TreatOutputAsContent>
97+
<Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Generating %(Filename).inc</Message>
98+
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Generating %(Filename).inc</Message>
99+
<LinkObjects Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</LinkObjects>
100+
<LinkObjects Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkObjects>
89101
</CustomBuild>
90102
<CustomBuild Include="shaderVS.hlsl">
91103
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
92104
<FileType>Document</FileType>
93105
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
94-
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">copy %(Identity) "$(OutDir)" &gt; NUL</Command>
95-
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">copy %(Identity) "$(OutDir)" &gt; NUL</Command>
96-
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)\%(Identity)</Outputs>
97-
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)\%(Identity)</Outputs>
98-
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</TreatOutputAsContent>
99-
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</TreatOutputAsContent>
106+
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">type $(ProjectDir)shaderStrHeader1.inc &gt; $(ProjectDir)%(Filename).inc
107+
echo g_%(Filename) &gt;&gt; $(ProjectDir)%(Filename).inc
108+
type $(ProjectDir)shaderStrHeader2.inc &gt;&gt; $(ProjectDir)%(Filename).inc
109+
type %(FullPath) &gt;&gt; $(ProjectDir)%(Filename).inc
110+
type $(ProjectDir)shaderStrFooter.inc &gt;&gt; $(ProjectDir)%(Filename).inc</Command>
111+
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">type $(ProjectDir)shaderStrHeader1.inc &gt; $(ProjectDir)%(Filename).inc
112+
echo g_%(Filename) &gt;&gt; $(ProjectDir)%(Filename).inc
113+
type $(ProjectDir)shaderStrHeader2.inc &gt;&gt; $(ProjectDir)%(Filename).inc
114+
type %(FullPath) &gt;&gt; $(ProjectDir)%(Filename).inc
115+
type $(ProjectDir)shaderStrFooter.inc &gt;&gt; $(ProjectDir)%(Filename).inc</Command>
116+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).inc</Outputs>
117+
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).inc</Outputs>
118+
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</TreatOutputAsContent>
119+
<TreatOutputAsContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</TreatOutputAsContent>
120+
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Generating %(Filename).inc</Message>
121+
<Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Generating %(Filename).inc</Message>
122+
<LinkObjects Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</LinkObjects>
123+
<LinkObjects Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkObjects>
100124
</CustomBuild>
101125
</ItemGroup>
102126
<ItemGroup>

HDR10Capture2019/MLDX12App.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include <shlwapi.h>
1818
#include <shellapi.h>
1919

20+
// shader source code as string
21+
#include "shaderVS.inc"
22+
#include "shaderColorConvPS.inc"
23+
2024
// D3D12HelloFrameBuffering sampleを改造して作成。
2125
//*********************************************************
2226
// Copyright (c) Microsoft. All rights reserved.
@@ -429,7 +433,10 @@ MLDX12App::LoadAssets(void)
429433
}
430434

431435
mPipelineState.Reset();
432-
MLDX12Common::SetupPSO(mDevice.Get(), mBackBufferFmt, mRootSignature.Get(), L"shaderVS.hlsl", L"shaderColorConvPS.hlsl", mPipelineState);
436+
MLDX12Common::SetupPSOFromMemory(mDevice.Get(), mBackBufferFmt, mRootSignature.Get(),
437+
"shaderVS", strlen(g_shaderVS), g_shaderVS,
438+
"shaderPS", strlen(g_shaderColorConvPS), g_shaderColorConvPS,
439+
mPipelineState);
433440
NAME_D3D12_OBJECT(mPipelineState);
434441

435442
mCmdList.Reset();

HDR10Capture2019/MLDX12Common.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,81 @@ MLDX12Common::SetupPSO(ID3D12Device *device, DXGI_FORMAT rtvFormat, ID3D12RootSi
7878
ThrowIfFailed(device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&pso)));
7979
}
8080

81+
void
82+
MLDX12Common::SetupPSOFromMemory(
83+
ID3D12Device* device, DXGI_FORMAT rtvFormat, ID3D12RootSignature* rootSignature,
84+
const char* vsShaderName, size_t vsShaderBytes, const char* vsShaderStr,
85+
const char* psShaderName, size_t psShaderBytes, const char* psShaderStr,
86+
ComPtr<ID3D12PipelineState>& pso)
87+
{
88+
WCHAR s[512];
89+
GetAssetsPath(s, _countof(s));
90+
std::wstring assetsPath = s;
91+
92+
ComPtr<ID3DBlob> vertexShader;
93+
ComPtr<ID3DBlob> pixelShader;
94+
95+
#if defined(_DEBUG)
96+
// Enable better shader debugging with the graphics debugging tools.
97+
UINT compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
98+
#else
99+
UINT compileFlags = 0;
100+
#endif
101+
102+
ComPtr<ID3DBlob> vsCompileMsg;
103+
ComPtr<ID3DBlob> psCompileMsg;
104+
HRESULT hr;
105+
hr = D3DCompile(vsShaderStr, vsShaderBytes, vsShaderName, nullptr, nullptr, "VSMain", "vs_5_0", compileFlags, 0, &vertexShader, &vsCompileMsg);
106+
if (FAILED(hr)) {
107+
char* s = (char*)vsCompileMsg->GetBufferPointer();
108+
OutputDebugStringA(s);
109+
ThrowIfFailed(hr);
110+
}
111+
hr = D3DCompile(psShaderStr, psShaderBytes, psShaderName, nullptr, nullptr, "PSMain", "ps_5_0", compileFlags, 0, &pixelShader, &psCompileMsg);
112+
if (FAILED(hr)) {
113+
char* s = (char*)psCompileMsg->GetBufferPointer();
114+
OutputDebugStringA(s);
115+
ThrowIfFailed(hr);
116+
}
117+
118+
D3D12_INPUT_ELEMENT_DESC inputElementDescs[] =
119+
{
120+
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
121+
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}
122+
};
123+
124+
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
125+
psoDesc.InputLayout = { inputElementDescs, _countof(inputElementDescs) };
126+
psoDesc.pRootSignature = rootSignature;
127+
psoDesc.VS = CD3DX12_SHADER_BYTECODE(vertexShader.Get());
128+
psoDesc.PS = CD3DX12_SHADER_BYTECODE(pixelShader.Get());
129+
psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
130+
131+
#if 1
132+
// 普通のアルファーブレンディング。
133+
{
134+
D3D12_BLEND_DESC& desc = psoDesc.BlendState;
135+
desc.AlphaToCoverageEnable = false;
136+
desc.RenderTarget[0].BlendEnable = true;
137+
desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;
138+
desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA;
139+
desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
140+
desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA;
141+
desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;
142+
desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
143+
desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
144+
}
145+
#else
146+
// ブレンディングなしの上書き。
147+
psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
148+
#endif
149+
psoDesc.DepthStencilState.DepthEnable = FALSE;
150+
psoDesc.DepthStencilState.StencilEnable = FALSE;
151+
psoDesc.SampleMask = UINT_MAX;
152+
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
153+
psoDesc.NumRenderTargets = 1;
154+
psoDesc.RTVFormats[0] = rtvFormat;
155+
psoDesc.SampleDesc.Count = 1;
156+
ThrowIfFailed(device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&pso)));
157+
}
158+

HDR10Capture2019/MLDX12Common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ using Microsoft::WRL::ComPtr;
77
class MLDX12Common {
88
public:
99
static void SetupPSO(ID3D12Device *device, DXGI_FORMAT rtvFormat, ID3D12RootSignature * rootSign, const wchar_t *vsShaderName, const wchar_t* psShaderName, ComPtr<ID3D12PipelineState> & pso);
10+
static void SetupPSOFromMemory(ID3D12Device* device, DXGI_FORMAT rtvFormat, ID3D12RootSignature* rootSign,
11+
const char * vsShaderName, size_t vsShaderBytes, const char* vsShaderStr,
12+
const char * psShaderName, size_t psShaderBytes, const char* psShaderStr,
13+
ComPtr<ID3D12PipelineState>& pso);
1014
};

HDR10Capture2019/shaderStrFooter.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
)ShaderStr";

HDR10Capture2019/shaderStrHeader1.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const char *

HDR10Capture2019/shaderStrHeader2.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
= R"ShaderStr(

0 commit comments

Comments
 (0)