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
320 lines (260 loc) · 12.2 KB
/
Example.cpp
File metadata and controls
320 lines (260 loc) · 12.2 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
/*
* Example.cpp (Example_Tessellation)
*
* This file is part of the "LLGL" project (Copyright (c) 2015-2019 by Lukas Hermanns)
* See "LICENSE.txt" for license information.
*/
#include <ExampleBase.h>
// Automatically rotate the model
//#define AUTO_ROTATE
// Use render pass to optimize attachment clearing
//#define ENABLE_RENDER_PASS
class Example_Tessellation : public ExampleBase
{
ShaderPipeline shaderPipeline;
LLGL::PipelineState* pipeline[2] = { nullptr };
LLGL::Buffer* vertexBuffer = nullptr;
LLGL::Buffer* indexBuffer = nullptr;
LLGL::Buffer* constantBuffer = nullptr;
LLGL::PipelineLayout* pipelineLayout = nullptr;
LLGL::ResourceHeap* resourceHeap = nullptr;
#ifdef ENABLE_RENDER_PASS
LLGL::RenderPass* renderPass = nullptr;
#endif
std::uint32_t constantBufferIndex = 0;
bool showWireframe = true;
struct Settings
{
Gs::Matrix4f wvpMatrix;
float tessLevelInner = 5.0f;
float tessLevelOuter = 5.0f;
float twist = 0.0f;
float _pad0; // <-- padding for 16 byte pack alignment of constant buffers
}
settings;
public:
Example_Tessellation() :
ExampleBase( L"LLGL Example: Tessellation" )
{
// Check if constant buffers and tessellation shaders are supported
const auto& renderCaps = renderer->GetRenderingCaps();
if (!renderCaps.features.hasConstantBuffers)
throw std::runtime_error("constant buffers are not supported by this renderer");
if (!renderCaps.features.hasTessellatorStage)
throw std::runtime_error("tessellation is not supported by this renderer");
// Create graphics object
auto vertexFormat = CreateBuffers();
LoadShaders(vertexFormat);
#ifdef ENABLE_RENDER_PASS
CreateRenderPass();
#endif
CreatePipelines();
// Print some information on the standard output
std::cout << "press LEFT MOUSE BUTTON and move mouse on X axis to increase/decrease inner tessellation" << std::endl;
std::cout << "press RIGHT MOUSE BUTTON and move mouse on X axis to increase/decrease outer tessellation" << std::endl;
std::cout << "press MIDDLE MOUSE BUTTON and move mouse on X axis to increase/decrease twist" << std::endl;
std::cout << "press TAB KEY to switch between wireframe modes" << std::endl;
ShowTessLevel();
}
LLGL::VertexFormat CreateBuffers()
{
// Specify vertex format
LLGL::VertexFormat vertexFormat;
vertexFormat.AppendAttribute({ "position", LLGL::Format::RGB32Float });
UpdateUserInput();
// Create buffers for a simple 3D cube model
vertexBuffer = CreateVertexBuffer(GenerateCubeVertices(), vertexFormat);
indexBuffer = CreateIndexBuffer(GenerateCubeQuadIndices(), LLGL::Format::R32UInt);
constantBuffer = CreateConstantBuffer(settings);
return vertexFormat;
}
void LoadShaders(const LLGL::VertexFormat& vertexFormat)
{
// Load shader program
if (Supported(LLGL::ShadingLanguage::GLSL))
{
shaderPipeline.vs = LoadShader({ LLGL::ShaderType::Vertex, "Example.vert" }, { vertexFormat });
shaderPipeline.hs = LoadShader({ LLGL::ShaderType::TessControl, "Example.tesc" });
shaderPipeline.ds = LoadShader({ LLGL::ShaderType::TessEvaluation, "Example.tese" });
shaderPipeline.ps = LoadShader({ LLGL::ShaderType::Fragment, "Example.frag" });
}
else if (Supported(LLGL::ShadingLanguage::SPIRV))
{
shaderPipeline.vs = LoadShader({ LLGL::ShaderType::Vertex, "Example.450core.vert.spv" }, { vertexFormat });
shaderPipeline.hs = LoadShader({ LLGL::ShaderType::TessControl, "Example.450core.tesc.spv" });
shaderPipeline.ds = LoadShader({ LLGL::ShaderType::TessEvaluation, "Example.450core.tese.spv" });
shaderPipeline.ps = LoadShader({ LLGL::ShaderType::Fragment, "Example.450core.frag.spv" });
}
else if (Supported(LLGL::ShadingLanguage::HLSL))
{
shaderPipeline.vs = LoadShader({ LLGL::ShaderType::Vertex, "Example.hlsl", "VS", "vs_5_0" }, { vertexFormat });
shaderPipeline.hs = LoadShader({ LLGL::ShaderType::TessControl, "Example.hlsl", "HS", "hs_5_0" });
shaderPipeline.ds = LoadShader({ LLGL::ShaderType::TessEvaluation, "Example.hlsl", "DS", "ds_5_0" });
shaderPipeline.ps = LoadShader({ LLGL::ShaderType::Fragment, "Example.hlsl", "PS", "ps_5_0" });
}
else if (Supported(LLGL::ShadingLanguage::Metal))
{
shaderPipeline.hs = LoadShader({ LLGL::ShaderType::Compute, "Example.metal", "HS", "2.0" });
shaderPipeline.ds = LoadShader({ LLGL::ShaderType::Vertex, "Example.metal", "DS", "2.0" }, { vertexFormat });
shaderPipeline.ps = LoadShader({ LLGL::ShaderType::Fragment, "Example.metal", "PS", "2.0" });
constantBufferIndex = 1;//TODO: unify
}
}
#ifdef ENABLE_RENDER_PASS
void CreateRenderPass()
{
LLGL::RenderPassDescriptor renderPassDesc;
{
renderPassDesc.colorAttachments[0] = LLGL::AttachmentFormatDescriptor{ swapChain->GetColorFormat(), LLGL::AttachmentLoadOp::Clear };
renderPassDesc.depthAttachment = LLGL::AttachmentFormatDescriptor{ swapChain->GetDepthStencilFormat(), LLGL::AttachmentLoadOp::Clear };
renderPassDesc.samples = GetMultiSampleDesc().SampleCount();
}
renderPass = renderer->CreateRenderPass(renderPassDesc);
}
#endif
void CreatePipelines()
{
// Create pipeline layout
LLGL::PipelineLayoutDescriptor plDesc;
{
plDesc.bindings =
{
LLGL::BindingDescriptor
{
"Settings",
LLGL::ResourceType::Buffer,
LLGL::BindFlags::ConstantBuffer,
(IsMetal() ? LLGL::StageFlags::ComputeStage | LLGL::StageFlags::VertexStage : LLGL::StageFlags::AllTessStages),
constantBufferIndex
}
};
}
pipelineLayout = renderer->CreatePipelineLayout(plDesc);
// Create resource view heap
LLGL::ResourceHeapDescriptor resourceHeapDesc;
{
resourceHeapDesc.pipelineLayout = pipelineLayout;
resourceHeapDesc.resourceViews = { constantBuffer };
}
resourceHeap = renderer->CreateResourceHeap(resourceHeapDesc);
// Setup graphics pipeline descriptors
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
// Set references to shader program, render pass, and pipeline layout
pipelineDesc.vertexShader = shaderPipeline.vs;
pipelineDesc.tessControlShader = shaderPipeline.hs;
pipelineDesc.tessEvaluationShader = shaderPipeline.ds;
pipelineDesc.fragmentShader = shaderPipeline.ps;
#ifdef ENABLE_RENDER_PASS
pipelineDesc.renderPass = renderPass;
#else
pipelineDesc.renderPass = swapChain->GetRenderPass();
#endif
pipelineDesc.pipelineLayout = pipelineLayout;
// Set input-assembler state (draw pachtes with 4 control points)
pipelineDesc.primitiveTopology = LLGL::PrimitiveTopology::Patches4;
// Enable multi-sample anti-aliasing
pipelineDesc.rasterizer.multiSampleEnabled = (GetSampleCount() > 1);
// Enable depth test and writing
pipelineDesc.depth.testEnabled = true;
pipelineDesc.depth.writeEnabled = true;
// Enable back-face culling
pipelineDesc.rasterizer.cullMode = LLGL::CullMode::Back;
// Specify tessellation state (only required for Metal)
pipelineDesc.tessellation.indexFormat = LLGL::Format::R32UInt;
pipelineDesc.tessellation.partition = LLGL::TessellationPartition::FractionalOdd;
pipelineDesc.tessellation.outputWindingCCW = true;
}
// Create graphics pipelines
pipeline[0] = renderer->CreatePipelineState(pipelineDesc);
pipelineDesc.rasterizer.polygonMode = LLGL::PolygonMode::Wireframe;
pipeline[1] = renderer->CreatePipelineState(pipelineDesc);
}
void ShowTessLevel()
{
//std::cout << "tessellation level (inner = " << settings.tessLevelInner << ", outer = " << settings.tessLevelOuter << ") \r";
//std::flush(std::cout);
}
private:
void UpdateUserInput()
{
// Tessellation level-of-detail limits
static const float tessLevelMin = 1.0f, tessLevelMax = 64.0f;
// Update tessellation levels by user input
auto motion = input.GetMouseMotion().x;
auto motionScaled = static_cast<float>(motion)*0.1f;
if (input.KeyPressed(LLGL::Key::LButton))
{
settings.tessLevelInner += motionScaled;
settings.tessLevelInner = Gs::Clamp(settings.tessLevelInner, tessLevelMin, tessLevelMax);
}
if (input.KeyPressed(LLGL::Key::RButton))
{
settings.tessLevelOuter += motionScaled;
settings.tessLevelOuter = Gs::Clamp(settings.tessLevelOuter, tessLevelMin, tessLevelMax);
}
if ( motion != 0 && ( input.KeyPressed(LLGL::Key::LButton) || input.KeyPressed(LLGL::Key::RButton) ) )
ShowTessLevel();
if (input.KeyPressed(LLGL::Key::MButton))
settings.twist += Gs::Deg2Rad(motionScaled);
if (input.KeyDown(LLGL::Key::Tab))
showWireframe = !showWireframe;
// Update matrices
Gs::Matrix4f worldMatrix;
Gs::Translate(worldMatrix, Gs::Vector3f(0, 0, 5));
settings.wvpMatrix = projection * worldMatrix;
#ifdef AUTO_ROTATE
static float rotation;
rotation += 0.0025f;
Gs::RotateFree(settings.worldMatrix, Gs::Vector3f(1, 1, 1).Normalized(), rotation);
#endif
}
void DrawScene()
{
commands->Begin();
{
// Update constant buffer
commands->UpdateBuffer(*constantBuffer, 0, &settings, sizeof(settings));
// Set hardware buffers to draw the model
commands->SetVertexBuffer(*vertexBuffer);
commands->SetIndexBuffer(*indexBuffer);
// Set the swap-chain as the initial render target
#ifdef ENABLE_RENDER_PASS
commands->BeginRenderPass(*swapChain, renderPass);
#else
commands->BeginRenderPass(*swapChain);
// Clear color- and depth buffers
commands->Clear(LLGL::ClearFlags::ColorDepth, backgroundColor);
#endif
{
// Set viewport
commands->SetViewport(swapChain->GetResolution());
// Set graphics pipeline with the shader
commands->SetPipelineState(*pipeline[showWireframe ? 1 : 0]);
if (resourceHeap)
{
// Bind resource view heap to graphics pipeline
commands->SetResourceHeap(*resourceHeap);
}
else
{
// Set constant buffer only to tessellation shader stages
commands->SetResource(*constantBuffer, constantBufferIndex, LLGL::BindFlags::ConstantBuffer, LLGL::StageFlags::AllTessStages);
}
// Draw tessellated quads with 24=4*6 vertices from patches of 4 control points
commands->DrawIndexed(24, 0);
}
commands->EndRenderPass();
}
commands->End();
commandQueue->Submit(*commands);
// Present result on the screen
swapChain->Present();
}
void OnDrawFrame() override
{
UpdateUserInput();
DrawScene();
}
};
LLGL_IMPLEMENT_EXAMPLE(Example_Tessellation);