forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.cpp
More file actions
329 lines (275 loc) · 13.6 KB
/
Example.cpp
File metadata and controls
329 lines (275 loc) · 13.6 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Example.cpp (Example_StencilBuffer)
*
* This file is part of the "LLGL" project (Copyright (c) 2015-2019 by Lukas Hermanns)
* See "LICENSE.txt" for license information.
*/
#include <ExampleBase.h>
class Example_StencilBuffer : public ExampleBase
{
LLGL::PipelineLayout* pipelineLayout = nullptr;
LLGL::ResourceHeap* resourceHeap = {};
LLGL::Shader* vsScene = nullptr;
LLGL::Shader* fsScene = nullptr;
LLGL::Shader* vsStencil = nullptr;
LLGL::PipelineState* pipelineScene = {};
LLGL::PipelineState* pipelineStencilWrite = {};
LLGL::PipelineState* pipelineStencilRead = {};
LLGL::Buffer* vertexBuffer = nullptr;
LLGL::Buffer* constantBuffer = nullptr;
TriangleMesh meshScene;
TriangleMesh meshPortal;
TriangleMesh meshObject1;
TriangleMesh meshObject2;
Gs::Vector3f objectPosition = { 0, -1, 3 };
float viewDistanceToCenter = 8.0f;
Gs::Vector2f viewRotation;
struct Settings
{
Gs::Matrix4f wMatrix;
Gs::Matrix4f vpMatrix;
Gs::Vector3f lightDir = Gs::Vector3f(-0.25f, -1.0f, 0.5f).Normalized();
float _pad1;
LLGL::ColorRGBAf diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
}
settings;
public:
Example_StencilBuffer() :
ExampleBase { L"LLGL Example: StencilBuffer" }
{
// Create all graphics objects
auto vertexFormat = CreateBuffers();
LoadShaders(vertexFormat);
CreatePipelineLayouts();
CreatePipelines();
CreateResourceHeaps();
#if 0
// Show some information
std::cout << "press LEFT MOUSE BUTTON and move the mouse on the X-axis to rotate the OUTER cube" << std::endl;
std::cout << "press RIGHT MOUSE BUTTON and move the mouse on the X-axis to rotate the INNER cube" << std::endl;
std::cout << "press RETURN KEY to save the render target texture to a PNG file" << std::endl;
#endif
}
private:
LLGL::VertexFormat CreateBuffers()
{
// Specify vertex format
LLGL::VertexFormat vertexFormat;
vertexFormat.AppendAttribute({ "position", LLGL::Format::RGB32Float });
vertexFormat.AppendAttribute({ "normal", LLGL::Format::RGB32Float });
vertexFormat.SetStride(sizeof(TexturedVertex));
// Load 3D models
std::vector<TexturedVertex> vertices;
meshScene = LoadObjModel(vertices, "../../Media/Models/Portal-Scene.obj");
meshPortal = LoadObjModel(vertices, "../../Media/Models/Portal-Stencil.obj");
meshObject1 = LoadObjModel(vertices, "../../Media/Models/WiredBox.obj");
meshObject2 = LoadObjModel(vertices, "../../Media/Models/Pyramid.obj");
meshObject1.color = { 0.2f, 0.9f, 0.1f };
meshObject2.color = { 0.9f, 0.1f, 0.2f };
// Create vertex, index, and constant buffer
vertexBuffer = CreateVertexBuffer(vertices, vertexFormat);
constantBuffer = CreateConstantBuffer(settings);
return vertexFormat;
}
void LoadShaders(const LLGL::VertexFormat& vertexFormat)
{
// Load shader program
if (Supported(LLGL::ShadingLanguage::HLSL))
{
vsScene = LoadShader({ LLGL::ShaderType::Vertex, "Example.hlsl", "VScene", "vs_5_0" }, { vertexFormat });
fsScene = LoadShader({ LLGL::ShaderType::Fragment, "Example.hlsl", "PScene", "ps_5_0" });
vsStencil = LoadShader({ LLGL::ShaderType::Vertex, "Example.hlsl", "VStencil", "vs_5_0" }, { vertexFormat });
}
else if (Supported(LLGL::ShadingLanguage::GLSL))
{
vsScene = LoadShader({ LLGL::ShaderType::Vertex, "Scene.vert" }, { vertexFormat });
fsScene = LoadShader({ LLGL::ShaderType::Fragment, "Scene.frag" });
vsStencil = LoadShader({ LLGL::ShaderType::Vertex, "Stencil.vert" }, { vertexFormat });
}
else if (Supported(LLGL::ShadingLanguage::SPIRV))
{
vsScene = LoadShader({ LLGL::ShaderType::Vertex, "Scene.450core.vert.spv" }, { vertexFormat });
fsScene = LoadShader({ LLGL::ShaderType::Fragment, "Scene.450core.frag.spv" });
vsStencil = LoadShader({ LLGL::ShaderType::Vertex, "Stencil.450core.vert.spv" }, { vertexFormat });
}
else if (Supported(LLGL::ShadingLanguage::Metal))
{
vsScene = LoadShader({ LLGL::ShaderType::Vertex, "Example.metal", "VScene", "1.1" }, { vertexFormat });
fsScene = LoadShader({ LLGL::ShaderType::Fragment, "Example.metal", "PScene", "1.1" });
vsStencil = LoadShader({ LLGL::ShaderType::Vertex, "Example.metal", "VStencil", "1.1" }, { vertexFormat });
}
else
throw std::runtime_error("shaders not supported for active renderer");
}
void CreatePipelineLayouts()
{
// Create pipeline layouts for shadow-map and scene rendering
pipelineLayout = renderer->CreatePipelineLayout(
LLGL::PipelineLayoutDesc("cbuffer(Settings@1):frag:vert")
);
}
void CreatePipelines()
{
// Create graphics pipeline for scene rendering
{
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.vertexShader = vsScene;
pipelineDesc.fragmentShader = fsScene;
pipelineDesc.renderPass = swapChain->GetRenderPass();
pipelineDesc.pipelineLayout = pipelineLayout;
pipelineDesc.depth.testEnabled = true;
pipelineDesc.depth.writeEnabled = true;
pipelineDesc.rasterizer.cullMode = LLGL::CullMode::Back;
pipelineDesc.rasterizer.multiSampleEnabled = (GetSampleCount() > 1);
}
pipelineScene = renderer->CreatePipelineState(pipelineDesc);
}
// Create graphics pipeline for stencil-write rendering
{
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.vertexShader = vsStencil;
pipelineDesc.renderPass = swapChain->GetRenderPass();
pipelineDesc.pipelineLayout = pipelineLayout;
pipelineDesc.depth.testEnabled = true; // Read all depth bits
pipelineDesc.depth.writeEnabled = false; // Write no depth bits
pipelineDesc.stencil.testEnabled = true; // Enable stencil test, even though we only write the stencil bits
pipelineDesc.stencil.front.depthPassOp = LLGL::StencilOp::Replace;
pipelineDesc.stencil.front.compareOp = LLGL::CompareOp::AlwaysPass;
pipelineDesc.stencil.front.reference = 1;
pipelineDesc.stencil.front.readMask = 0u; // Read no stencil bits
pipelineDesc.stencil.front.writeMask = ~0u; // Write all stencil bits
pipelineDesc.rasterizer.cullMode = LLGL::CullMode::Back;
pipelineDesc.rasterizer.multiSampleEnabled = (GetSampleCount() > 1);
pipelineDesc.blend.targets[0].colorMask = { false, false, false, false }; // Write no color bits
}
pipelineStencilWrite = renderer->CreatePipelineState(pipelineDesc);
}
// Create graphics pipeline for stencil-read rendering
{
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.vertexShader = vsScene;
pipelineDesc.fragmentShader = fsScene;
pipelineDesc.renderPass = swapChain->GetRenderPass();
pipelineDesc.pipelineLayout = pipelineLayout;
pipelineDesc.depth.testEnabled = true;
pipelineDesc.depth.writeEnabled = true; // Write all depth bits
pipelineDesc.stencil.testEnabled = true;
pipelineDesc.stencil.referenceDynamic = true; // Change stencil reference independently of PSO
pipelineDesc.stencil.front.compareOp = LLGL::CompareOp::Equal;
pipelineDesc.stencil.front.readMask = ~0u; // Read all stencil bits
pipelineDesc.stencil.front.writeMask = 0u; // Write no stencil bits
pipelineDesc.rasterizer.cullMode = LLGL::CullMode::Back;
pipelineDesc.rasterizer.multiSampleEnabled = (GetSampleCount() > 1);
}
pipelineStencilRead = renderer->CreatePipelineState(pipelineDesc);
}
}
void CreateResourceHeaps()
{
// Create resource heap for scene rendering
LLGL::ResourceHeapDescriptor resourceHeapDesc;
{
resourceHeapDesc.pipelineLayout = pipelineLayout;
resourceHeapDesc.resourceViews = { constantBuffer };
}
resourceHeap = renderer->CreateResourceHeap(resourceHeapDesc);
}
void UpdateScene()
{
static float animation;
// Update animation
if (input.KeyPressed(LLGL::Key::LButton))
{
auto motion = input.GetMouseMotion();
viewRotation.x += static_cast<float>(motion.y) * 0.25f;
viewRotation.x = Gs::Clamp(viewRotation.x, -45.0f, 0.0f);
viewRotation.y += static_cast<float>(motion.x) * 0.25f;
}
if (input.KeyPressed(LLGL::Key::RButton))
{
auto motion = input.GetMouseMotion();
animation += static_cast<float>(motion.x) * 0.25f;
}
// Update model transform
meshObject1.transform.LoadIdentity();
Gs::Translate(meshObject1.transform, objectPosition);
Gs::RotateFree(meshObject1.transform, Gs::Vector3f{ 0, 1, 0 }, Gs::pi + Gs::Deg2Rad(animation));
meshObject2.transform.LoadIdentity();
Gs::Translate(meshObject2.transform, objectPosition);
Gs::RotateFree(meshObject2.transform, Gs::Vector3f{ 0, 1, 0 }, Gs::pi + Gs::Deg2Rad(animation));
// Update view transformation
settings.vpMatrix.LoadIdentity();
//Gs::RotateFree(settings.vpMatrix, { 1, 0, 0 }, Gs::pi*0.5f);
Gs::RotateFree(settings.vpMatrix, { 0, 1, 0 }, Gs::Deg2Rad(viewRotation.y));
Gs::RotateFree(settings.vpMatrix, { 1, 0, 0 }, Gs::Deg2Rad(viewRotation.x));
Gs::Translate(settings.vpMatrix, { 0, 0, -viewDistanceToCenter });
settings.vpMatrix.MakeInverse();
settings.vpMatrix = projection * settings.vpMatrix;
}
void RenderMesh(const TriangleMesh& mesh)
{
settings.wMatrix = mesh.transform;
settings.diffuse = mesh.color;
commands->UpdateBuffer(*constantBuffer, 0, &settings, sizeof(settings));
commands->Draw(mesh.numVertices, mesh.firstVertex);
}
void RenderScene()
{
// Clear entire framebuffer, i.e. color, depth, and stencil buffers
commands->Clear(LLGL::ClearFlags::All, { backgroundColor });
// Render scene background
commands->SetPipelineState(*pipelineScene);
commands->SetResourceHeap(*resourceHeap);
RenderMesh(meshScene);
}
void RenderPortalStencil()
{
// Render portal stencil (no color is written)
commands->SetPipelineState(*pipelineStencilWrite);
RenderMesh(meshPortal);
}
void RenderSceneBetweenPortal()
{
commands->SetPipelineState(*pipelineStencilRead);
// Render scene objects outside portal (stencil = 0)
commands->SetStencilReference(0);
RenderMesh(meshObject1);
// Render scene objects inside portal (stencil = 1)
commands->SetStencilReference(1);
RenderMesh(meshObject2);
}
void OnDrawFrame() override
{
// Update scene by user input
UpdateScene();
commands->Begin();
{
// Bind common input assembly
commands->SetVertexBuffer(*vertexBuffer);
// Render everything directly into the swap-chain
commands->BeginRenderPass(*swapChain);
{
commands->SetViewport(swapChain->GetResolution());
// Draw scene, then draw the portal into the stencil buffer, and finally draw the hidden object inside the portal
commands->PushDebugGroup("Scene Pass (Render Background)");
RenderScene();
commands->PopDebugGroup();
commands->PushDebugGroup("Stencil Write Pass (Render Portal)");
RenderPortalStencil();
commands->PopDebugGroup();
commands->PushDebugGroup("Stencil Read Pass (Between Portal)");
RenderSceneBetweenPortal();
commands->PopDebugGroup();
}
commands->EndRenderPass();
}
commands->End();
commandQueue->Submit(*commands);
// Present result on the screen
swapChain->Present();
}
};
LLGL_IMPLEMENT_EXAMPLE(Example_StencilBuffer);