Repository navigation
Expand file tree
/
Copy pathDefaultShader.cpp
More file actions
218 lines (185 loc) · 7.77 KB
/
Copy pathDefaultShader.cpp
File metadata and controls
218 lines (185 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//////////////////////////////////////////////////////////////////////////////////
// Copyright (C) Inspiration Byte
// 2009-2020
//////////////////////////////////////////////////////////////////////////////////
// Description: Default MatSystem shader
// free of states, textures. Just uses matrix and fog setup
//////////////////////////////////////////////////////////////////////////////////
#include "core/core_common.h"
#include "materialsystem1/IMaterialSystem.h"
#include "materialsystem1/BaseShader.h"
static uint GenDefaultPipelineId(ArrayCRef<ETextureFormat> colorTargetFormat, ETextureFormat depthTargetFormat, const MatSysDefaultRenderPass& renderPassInfo, EPrimTopology primitiveTopology)
{
const uint pipelineFlags =
static_cast<uint>(renderPassInfo.cullMode)
| (renderPassInfo.depthTest << 2)
| (renderPassInfo.depthWrite << 3)
| (renderPassInfo.depthFunc << 4)
| (static_cast<uint>(primitiveTopology) << 7)
| (renderPassInfo.blendMode << 10);
uint hash = pipelineFlags;
hash *= 31;
hash += pipelineFlags;
for (int i = 0; i < colorTargetFormat.numElem(); ++i)
{
const ETextureFormat format = colorTargetFormat[i];
if (format == FORMAT_NONE)
break;
hash *= 31;
hash += static_cast<uint>(i);
hash *= 31;
hash += static_cast<uint>(format);
}
hash *= 31;
hash += static_cast<uint>(depthTargetFormat);
return hash;
}
//------------------------------------------------------------
BEGIN_SHADER_CLASS(
Default,
SHADER_VERTEX_ID(DynMeshVertex)
)
SHADER_INIT_PARAMS()
{
}
SHADER_INIT_RESOURCES()
{
SHADER_PARAM_TEXTURE_FIND(BaseTexture, m_baseTexture);
}
bool SetupRenderPass(IShaderAPI* renderAPI, const PipelineInputParams& pipelineParams, ArrayCRef<RenderBufferInfo> uniformBuffers, const RenderPassContext& passContext, IMaterial* originalMaterial) override
{
const MatSysDefaultRenderPass* rendPassInfo = static_cast<const MatSysDefaultRenderPass*>(passContext.data);
if (!rendPassInfo)
{
ASSERT_FAIL("No render pass data specified");
return false;
}
ASSERT_MSG(rendPassInfo->type == RENDERPASS_DEFAULT, "Must specify MatSysDefaultRenderPass in userData when drawing with default material");
const uint pipelineId = GenDefaultPipelineId(passContext.recorder->GetRenderTargetFormats(), passContext.recorder->GetDepthTargetFormat(), *rendPassInfo, pipelineParams.primitiveTopology);
auto it = m_renderPipelines.find(pipelineId);
if (it.atEnd())
{
// prepare basic pipeline descriptor
RenderPipelineDesc renderPipelineDesc = Builder<RenderPipelineDesc>()
.ShaderName(GetName())
.ShaderVertexLayoutId(pipelineParams.meshInstFormat.formatId)
.End();
for (const VertexLayoutDesc& layoutDesc : pipelineParams.meshInstFormat.layout)
renderPipelineDesc.vertex.vertexLayout.append(layoutDesc);
const ETextureFormat depthTargetFormat = passContext.recorder->GetDepthTargetFormat();
if (depthTargetFormat != FORMAT_NONE)
{
if (m_flags & MATERIAL_FLAG_DECAL)
g_matSystem->GetPolyOffsetSettings(renderPipelineDesc.depthStencil.depthBias, renderPipelineDesc.depthStencil.depthBiasSlopeScale);
// TODO: must be selective whenever Z test is on
Builder<DepthStencilStateParams>(renderPipelineDesc.depthStencil)
.DepthTestOn(rendPassInfo->depthTest)
.DepthFunction(rendPassInfo->depthFunc)
.DepthWriteOn(rendPassInfo->depthWrite && (m_flags & MATERIAL_FLAG_NO_Z_WRITE) == 0)
.DepthFormat(depthTargetFormat);
}
{
BlendStateParams colorBlend;
BlendStateParams alphaBlend;
switch (rendPassInfo->blendMode)
{
case SHADER_BLEND_TRANSLUCENT:
colorBlend = BlendStateTranslucent;
alphaBlend = BlendStateTranslucentAlpha;
break;
case SHADER_BLEND_ADDITIVE:
colorBlend = BlendStateAdditive;
alphaBlend = BlendStateAdditive;
break;
case SHADER_BLEND_MODULATE:
colorBlend = BlendStateModulate;
alphaBlend = BlendStateModulate; // TODO: figure out
break;
}
Builder<FragmentPipelineDesc> pipelineBuilder(renderPipelineDesc.fragment);
for (int i = 0; i < MAX_RENDERTARGETS; ++i)
{
const ETextureFormat format = passContext.recorder->GetRenderTargetFormats()[i];
if (format == FORMAT_NONE)
break;
if (rendPassInfo->blendMode != SHADER_BLEND_NONE)
pipelineBuilder.ColorTarget("CT", format, colorBlend, alphaBlend);
else
pipelineBuilder.ColorTarget("CT", format);
}
pipelineBuilder.End();
}
const bool isStripIdx = pipelineParams.primitiveTopology == PRIM_TRIANGLE_STRIP || pipelineParams.primitiveTopology == PRIM_LINE_STRIP;
Builder<PrimitiveDesc>(renderPipelineDesc.primitive)
.Topology(pipelineParams.primitiveTopology)
.Cull(rendPassInfo->cullMode)
.StripIndex(isStripIdx ? STRIPINDEX_UINT16 : STRIPINDEX_NONE)
.End();
it = m_renderPipelines.insert(pipelineId);
PipelineInfo& newPipelineInfo = *it;
newPipelineInfo.vertexLayoutId = pipelineParams.meshInstFormat.formatId;
newPipelineInfo.pipeline = renderAPI->CreateRenderPipeline(renderPipelineDesc);
}
const PipelineInfo& pipelineInfo = *it;
if (!pipelineInfo.pipeline)
return false;
const BindGroupSetupParams setupParams{
uniformBuffers,
pipelineInfo,
passContext,
originalMaterial,
nullptr,
pipelineId
};
passContext.recorder->SetPipeline(pipelineInfo.pipeline);
passContext.recorder->SetBindGroup(BINDGROUP_CONSTANT, GetBindGroup(renderAPI, BINDGROUP_CONSTANT, setupParams));
return true;
}
// this function returns material group.
// Default material has transient all transient resources
// as it's used for immediate drawing
IGPUBindGroupPtr GetBindGroup(IShaderAPI* renderAPI, EBindGroupId bindGroupId, const BindGroupSetupParams& setupParams) const override
{
if (bindGroupId == BINDGROUP_CONSTANT)
{
const MatSysDefaultRenderPass* rendPassInfo = static_cast<const MatSysDefaultRenderPass*>(setupParams.passContext.data);
ASSERT_MSG(rendPassInfo, "Must specify MatSysDefaultRenderPass in userData when drawing with default material");
TextureView whiteTexView = g_matSystem->GetWhiteTexture();
const TextureView& baseTexture = rendPassInfo->texture ? rendPassInfo->texture : whiteTexView;
// can use either fixed array or CMemoryStream with on-stack storage
FixedArray<Vector4D, 4> bufferData;
bufferData.append(rendPassInfo->drawColor);
bufferData.append(GetTextureTransform(m_baseTextureTransformVar, m_baseTextureScaleVar));
GPUBufferView cameraParamsBuffer;
for (const RenderBufferInfo& rendBuffer : setupParams.uniformBuffers)
{
if (rendBuffer.signature == s_matSysCameraBufferId)
cameraParamsBuffer = rendBuffer.bufferView;
}
if (!cameraParamsBuffer)
{
cameraParamsBuffer = m_currentCameraBuffer;
MatSysCamera cameraParams;
const int cameraChangeId = g_matSystem->GetCameraParams(cameraParams);
if (m_currentCameraId != cameraChangeId)
{
m_currentCameraId = cameraChangeId;
m_currentCameraBuffer = g_matSystem->GetTransientUniformBuffer(&cameraParams, sizeof(cameraParams));
cameraParamsBuffer = m_currentCameraBuffer;
}
}
GPUBufferView materialParamsBuffer = g_matSystem->GetTransientUniformBuffer(bufferData.ptr(), sizeof(bufferData[0]) * bufferData.numElem());
BindGroupDesc bindGroupDesc = Builder<BindGroupDesc>()
.Buffer(StringIdConst24("camera"), cameraParamsBuffer)
.Buffer(StringIdConst24("materialParams"), materialParamsBuffer)
.Sampler(StringIdConst24("BaseTextureSampler"), baseTexture.texture->GetSamplerState())
.Texture(StringIdConst24("BaseTexture"), baseTexture)
.End();
return CreateBindGroup(bindGroupDesc, bindGroupId, renderAPI, setupParams.pipelineInfo);
}
return setupParams.pipelineInfo.bindGroup[bindGroupId];
}
MatTextureProxy m_baseTexture;
mutable GPUBufferView m_currentCameraBuffer;
mutable int m_currentCameraId{ -1 };
END_SHADER_CLASS