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
315 lines (270 loc) · 12.2 KB
/
Example.cpp
File metadata and controls
315 lines (270 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
/*
* Example.cpp (Example_MultiContext)
*
* This file is part of the "LLGL" project (Copyright (c) 2015-2019 by Lukas Hermanns)
* See "LICENSE.txt" for license information.
*/
#include <ExampleBase.h>
int main(int argc, char* argv[])
{
try
{
// Set report callback to standard output
LLGL::Log::SetReportCallbackStd();
// Load render system module
LLGL::RenderingDebugger debugger;
auto renderer = LLGL::RenderSystem::Load(GetSelectedRendererModule(argc, argv));//, nullptr, &debugger);
std::cout << "LLGL Renderer: " << renderer->GetName() << std::endl;
// Create two swap-chains
LLGL::SwapChainDescriptor swapChainDesc[2];
{
swapChainDesc[0].resolution = { 640, 480 };
swapChainDesc[0].samples = 8;
swapChainDesc[0].depthBits = 0;
swapChainDesc[0].stencilBits = 0;
}
auto swapChain1 = renderer->CreateSwapChain(swapChainDesc[0]);
{
swapChainDesc[1].resolution = { 640, 480 };
swapChainDesc[1].samples = 8;//8;
swapChainDesc[1].depthBits = 0;
swapChainDesc[1].stencilBits = 0;
}
auto swapChain2 = renderer->CreateSwapChain(swapChainDesc[1]);
// Enable V-sync
swapChain1->SetVsyncInterval(1);
swapChain2->SetVsyncInterval(1);
// Get command queue and create command buffer
auto commandQueue = renderer->GetCommandQueue();
auto commands = renderer->CreateCommandBuffer();
// Create input handler
auto& window1 = static_cast<LLGL::Window&>(swapChain1->GetSurface());
auto& window2 = static_cast<LLGL::Window&>(swapChain2->GetSurface());
LLGL::Input inputs[2];
inputs[0].Listen(window1);
inputs[1].Listen(window2);
// Set window titles
window1.SetTitle(L"LLGL Example: Multi Context (1)");
window2.SetTitle(L"LLGL Example: Multi Context (2)");
// Set window positions
LLGL::Extent2D desktopResolution;
if (auto display = LLGL::Display::GetPrimary())
desktopResolution = display->GetDisplayMode().resolution;
const LLGL::Offset2D desktopCenter
{
static_cast<int>(desktopResolution.width)/2,
static_cast<int>(desktopResolution.height)/2
};
window1.SetPosition({ desktopCenter.x - 700, desktopCenter.y - 480/2 });
window2.SetPosition({ desktopCenter.x + 700 - 640, desktopCenter.y - 480/2 });
// Show windows
window1.Show();
window2.Show();
// Vertex data structure
struct Vertex
{
float position[2];
float color[3];
};
// Vertex data
float objSize = 0.5f;
Vertex vertices[] =
{
// Triangle
{ { 0, objSize }, { 1, 0, 0 } },
{ { objSize, -objSize }, { 0, 1, 0 } },
{ { -objSize, -objSize }, { 0, 0, 1 } },
// Quad
{ { -objSize, -objSize }, { 1, 0, 0 } },
{ { -objSize, objSize }, { 1, 0, 0 } },
{ { objSize, -objSize }, { 1, 1, 0 } },
{ { objSize, objSize }, { 1, 1, 0 } },
};
// Vertex format
LLGL::VertexFormat vertexFormat;
vertexFormat.AppendAttribute({ "position", LLGL::Format::RG32Float }); // position has 2 float components
vertexFormat.AppendAttribute({ "color", LLGL::Format::RGB32Float }); // color has 3 float components
// Create vertex buffer
LLGL::BufferDescriptor vertexBufferDesc;
{
vertexBufferDesc.size = sizeof(vertices); // Size (in bytes) of the vertex buffer
vertexBufferDesc.bindFlags = LLGL::BindFlags::VertexBuffer;
vertexBufferDesc.vertexAttribs = vertexFormat.attributes; // Vertex format layout
}
auto vertexBuffer = renderer->CreateBuffer(vertexBufferDesc, vertices);
// Create shaders
LLGL::Shader* vertShader = nullptr;
LLGL::Shader* geomShader = nullptr;
LLGL::Shader* fragShader = nullptr;
// Load vertex, geometry, and fragment shaders from file
auto HasLanguage = [&](const LLGL::ShadingLanguage lang)
{
const auto& languages = renderer->GetRenderingCaps().shadingLanguages;
return (std::find(languages.begin(), languages.end(), lang) != languages.end());
};
LLGL::ShaderDescriptor vertShaderDesc, geomShaderDesc, fragShaderDesc;
if (HasLanguage(LLGL::ShadingLanguage::GLSL))
{
vertShaderDesc = { LLGL::ShaderType::Vertex, "Example.vert" };
geomShaderDesc = { LLGL::ShaderType::Geometry, "Example.geom" };
fragShaderDesc = { LLGL::ShaderType::Fragment, "Example.frag" };
}
else if (HasLanguage(LLGL::ShadingLanguage::SPIRV))
{
vertShaderDesc = LLGL::ShaderDescFromFile(LLGL::ShaderType::Vertex, "Example.450core.vert.spv");
geomShaderDesc = LLGL::ShaderDescFromFile(LLGL::ShaderType::Geometry, "Example.450core.geom.spv");
fragShaderDesc = LLGL::ShaderDescFromFile(LLGL::ShaderType::Fragment, "Example.450core.frag.spv");
}
else if (HasLanguage(LLGL::ShadingLanguage::HLSL))
{
vertShaderDesc = { LLGL::ShaderType::Vertex, "Example.hlsl", "VS", "vs_4_0" };
geomShaderDesc = { LLGL::ShaderType::Geometry, "Example.hlsl", "GS", "gs_4_0" };
fragShaderDesc = { LLGL::ShaderType::Fragment, "Example.hlsl", "PS", "ps_4_0" };
}
else if (HasLanguage(LLGL::ShadingLanguage::Metal))
{
vertShaderDesc = { LLGL::ShaderType::Vertex, "Example.metal", "VS", "2.0" };
//geomShaderDesc = N/A
fragShaderDesc = { LLGL::ShaderType::Fragment, "Example.metal", "PS", "2.0" };
}
// Set vertex input attributes and create vertex shader
vertShaderDesc.vertex.inputAttribs = vertexFormat.attributes;
vertShader = renderer->CreateShader(vertShaderDesc);
// Create geometry shader (if supported)
if (geomShaderDesc.source != nullptr)
geomShader = renderer->CreateShader(geomShaderDesc);
// Create fragment shader
fragShader = renderer->CreateShader(fragShaderDesc);
// Print info log (warnings and errors)
for (auto shader : { vertShader, geomShader, fragShader })
{
if (shader)
{
if (auto report = shader->GetReport())
{
if (*report->GetText() != '\0')
std::cerr << report->GetText() << std::endl;
}
}
}
// Create graphics pipeline
LLGL::PipelineState* pipeline[2] = {};
const bool logicOpSupported = renderer->GetRenderingCaps().features.hasLogicOp;
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.vertexShader = vertShader;
pipelineDesc.geometryShader = geomShader;
pipelineDesc.fragmentShader = fragShader;
pipelineDesc.renderPass = swapChain1->GetRenderPass();
pipelineDesc.primitiveTopology = LLGL::PrimitiveTopology::TriangleStrip;
pipelineDesc.rasterizer.multiSampleEnabled = (swapChainDesc[0].samples > 1);
}
pipeline[0] = renderer->CreatePipelineState(pipelineDesc);
{
pipelineDesc.renderPass = swapChain2->GetRenderPass();
pipelineDesc.rasterizer.multiSampleEnabled = (swapChainDesc[1].samples > 1);
// Only enable logic operations if it's supported, otherwise an exception is thrown
if (logicOpSupported)
pipelineDesc.blend.logicOp = LLGL::LogicOp::CopyInverted;
}
pipeline[1] = renderer->CreatePipelineState(pipelineDesc);
for (auto p : pipeline)
{
if (auto report = p->GetReport())
{
if (report->HasErrors())
throw std::runtime_error(report->GetText());
}
}
// Initialize viewport array
LLGL::Viewport viewports[2] =
{
LLGL::Viewport { 0.0f, 0.0f, 320.0f, 480.0f },
LLGL::Viewport { 320.0f, 0.0f, 320.0f, 480.0f },
};
const LLGL::ColorRGBAf backgroundColor[2] =
{
{ 0.2f, 0.2f, 0.5f, 1 },
{ 0.5f, 0.2f, 0.2f, 1 },
};
bool enableLogicOp[2] = { false, false };
if (logicOpSupported)
std::cout << "Press SPACE to enabled/disable logic fragment operations" << std::endl;
// Generate multiple-instances via geometry shader.
// Otherwise, use instanced rendering if geometry shaders are not supported (for Metal shading language).
std::uint32_t numInstances = (geomShader != nullptr ? 1 : 2);
// Enter main loop
while (!(inputs[0].KeyPressed(LLGL::Key::Escape) || inputs[1].KeyPressed(LLGL::Key::Escape)))
{
// Process events of both windows and quit when both windows are closed
const bool win1Processed = window1.ProcessEvents();
const bool win2Processed = window2.ProcessEvents();
if (!(win1Processed || win2Processed))
break;
// Switch between pipeline states
for (int i = 0; i < 2; ++i)
{
if (inputs[i].KeyDown(LLGL::Key::Space))
{
if (logicOpSupported)
{
std::cout << "Logic Fragment Operation ";
enableLogicOp[i] = !enableLogicOp[i];
if (enableLogicOp[i])
std::cout << "Enabled";
else
std::cout << "Disabled";
std::cout << " (Window " << (i + 1) << ")" << std::endl;
}
else
std::cout << "Logic Fragment Operation Not Supported" << std::endl;
}
}
// Start encoding commands
commands->Begin();
{
// Set global render states: viewports, vertex buffer, and graphics pipeline
//commands->SetVertexBuffer(*vertexBuffer);
// Draw triangle with 3 vertices in 1st swap-chain
if (window1.IsShown())
{
commands->BeginRenderPass(*swapChain1);
{
commands->Clear(LLGL::ClearFlags::Color, backgroundColor[0]);
commands->SetPipelineState(*pipeline[0]);//[enableLogicOp[0] ? 1 : 0]);
commands->SetViewports(2, viewports);
commands->SetVertexBuffer(*vertexBuffer);
commands->DrawInstanced(3, 0, numInstances);
}
commands->EndRenderPass();
}
// Draw quad with 4 vertices in 2nd swap-chain
if (window2.IsShown())
{
commands->BeginRenderPass(*swapChain2);
{
commands->Clear(LLGL::ClearFlags::Color, backgroundColor[1]);
commands->SetPipelineState(*pipeline[1]);//[enableLogicOp[1] ? 1 : 0]);
commands->SetViewports(2, viewports);
commands->SetVertexBuffer(*vertexBuffer);
commands->DrawInstanced(3, 0, numInstances);
//commands->DrawInstanced(4, 3, numInstances);
}
commands->EndRenderPass();
}
}
commands->End();
commandQueue->Submit(*commands);
// Present the results on the screen
if (window1.IsShown())
swapChain1->Present();
if (window2.IsShown())
swapChain2->Present();
}
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}