forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.cpp
271 lines (225 loc) · 10.2 KB
/
Example.cpp
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
/*
* Example.cpp (Example_HelloTriangle)
*
* This file is part of the "LLGL" project (Copyright (c) 2015-2019 by Lukas Hermanns)
* See "LICENSE.txt" for license information.
*/
#include <ExampleBase.h>
#include <chrono>
// Enable multi-sampling render context
#define ENABLE_MULTISAMPLING
// Enable timer to show render times every second
//#define ENABLE_TIMER
// Enable caching of pipeline state objects (PSO)
#define ENABLE_CACHED_PSO
int main(int argc, char* argv[])
{
try
{
// Let the user choose an available renderer
std::string rendererModule = GetSelectedRendererModule(argc, argv);
// Load render system module
std::unique_ptr<LLGL::RenderSystem> renderer = LLGL::RenderSystem::Load(rendererModule);
// Create render context
LLGL::RenderContextDescriptor contextDesc;
{
contextDesc.videoMode.resolution = { 800, 600 };
#ifdef ENABLE_MULTISAMPLING
contextDesc.samples = 8; // check if LLGL adapts sample count that is too high
#endif
contextDesc.vsyncInterval = 1;
}
LLGL::RenderContext* context = renderer->CreateRenderContext(contextDesc);
// Print renderer information
const auto& info = renderer->GetRendererInfo();
std::cout << "Renderer: " << info.rendererName << std::endl;
std::cout << "Device: " << info.deviceName << std::endl;
std::cout << "Vendor: " << info.vendorName << std::endl;
std::cout << "Shading Language: " << info.shadingLanguageName << std::endl;
// Set window title and show window
auto& window = LLGL::CastTo<LLGL::Window>(context->GetSurface());
window.SetTitle(L"LLGL Example: Hello Triangle");
window.Show();
// Vertex data structure
struct Vertex
{
float position[2];
uint8_t color[4];
};
// Vertex data (3 vertices for our triangle)
const float s = 0.5f;
Vertex vertices[] =
{
{ { 0, s }, { 255, 0, 0, 255 } }, // 1st vertex: center-top, red
{ { s, -s }, { 0, 255, 0, 255 } }, // 2nd vertex: right-bottom, green
{ { -s, -s }, { 0, 0, 255, 255 } }, // 3rd vertex: left-bottom, blue
};
// Vertex format
LLGL::VertexFormat vertexFormat;
// Append 2D float vector for position attribute
vertexFormat.AppendAttribute({ "position", LLGL::Format::RG32Float });
// Append 3D unsigned byte vector for color
vertexFormat.AppendAttribute({ "color", LLGL::Format::RGBA8UNorm });
// Update stride in case out vertex structure is not 4-byte aligned
vertexFormat.SetStride(sizeof(Vertex));
// Create vertex buffer
LLGL::BufferDescriptor vertexBufferDesc;
{
vertexBufferDesc.size = sizeof(vertices); // Size (in bytes) of the vertex buffer
vertexBufferDesc.bindFlags = LLGL::BindFlags::VertexBuffer; // Enables the buffer to be bound to a vertex buffer slot
vertexBufferDesc.vertexAttribs = vertexFormat.attributes; // Vertex format layout
}
LLGL::Buffer* vertexBuffer = renderer->CreateBuffer(vertexBufferDesc, vertices);
// Create shaders
LLGL::Shader* vertShader = nullptr;
LLGL::Shader* fragShader = nullptr;
const auto& languages = renderer->GetRenderingCaps().shadingLanguages;
LLGL::ShaderDescriptor vertShaderDesc, fragShaderDesc;
if (std::find(languages.begin(), languages.end(), LLGL::ShadingLanguage::GLSL) != languages.end())
{
if (std::find(languages.begin(), languages.end(), LLGL::ShadingLanguage::GLSL_140) != languages.end())
{
#ifdef __APPLE__
vertShaderDesc = { LLGL::ShaderType::Vertex, "Example.140core.vert" };
fragShaderDesc = { LLGL::ShaderType::Fragment, "Example.140core.frag" };
#else
vertShaderDesc = { LLGL::ShaderType::Vertex, "Example.vert" };
fragShaderDesc = { LLGL::ShaderType::Fragment, "Example.frag" };
#endif
}
else
{
vertShaderDesc = { LLGL::ShaderType::Vertex, "Example.120.vert" };
fragShaderDesc = { LLGL::ShaderType::Fragment, "Example.120.frag" };
}
}
else if (std::find(languages.begin(), languages.end(), LLGL::ShadingLanguage::SPIRV) != languages.end())
{
vertShaderDesc = LLGL::ShaderDescFromFile(LLGL::ShaderType::Vertex, "Example.450core.vert.spv");
fragShaderDesc = LLGL::ShaderDescFromFile(LLGL::ShaderType::Fragment, "Example.450core.frag.spv");
}
else if (std::find(languages.begin(), languages.end(), LLGL::ShadingLanguage::HLSL) != languages.end())
{
vertShaderDesc = { LLGL::ShaderType::Vertex, "Example.hlsl", "VS", "vs_4_0" };
fragShaderDesc = { LLGL::ShaderType::Fragment, "Example.hlsl", "PS", "ps_4_0" };
}
else if (std::find(languages.begin(), languages.end(), LLGL::ShadingLanguage::Metal) != languages.end())
{
vertShaderDesc = { LLGL::ShaderType::Vertex, "Example.metal", "VS", "1.1" };
fragShaderDesc = { LLGL::ShaderType::Fragment, "Example.metal", "PS", "1.1" };
}
// Specify vertex attributes for vertex shader
vertShaderDesc.vertex.inputAttribs = vertexFormat.attributes;
vertShader = renderer->CreateShader(vertShaderDesc);
fragShader = renderer->CreateShader(fragShaderDesc);
for (auto shader : { vertShader, fragShader })
{
if (shader != nullptr)
{
std::string log = shader->GetReport();
if (!log.empty())
std::cerr << log << std::endl;
}
}
// Create shader program which is used as composite
LLGL::ShaderProgramDescriptor shaderProgramDesc;
{
shaderProgramDesc.vertexShader = vertShader;
shaderProgramDesc.fragmentShader = fragShader;
}
LLGL::ShaderProgram* shaderProgram = renderer->CreateShaderProgram(shaderProgramDesc);
// Link shader program and check for errors
if (shaderProgram->HasErrors())
throw std::runtime_error(shaderProgram->GetReport());
// Create graphics pipeline
LLGL::PipelineState* pipeline = nullptr;
std::unique_ptr<LLGL::Blob> pipelineCache;
#ifdef ENABLE_CACHED_PSO
// Try to read PSO cache from file
const std::string cacheFilename = "GraphicsPSO." + rendererModule + ".cache";
if ((pipelineCache = LLGL::Blob::CreateFromFile(cacheFilename)) != nullptr)
{
// Create graphics PSO from cache
pipeline = renderer->CreatePipelineState(*pipelineCache);
std::cout << "Pipeline cache restored: " << pipelineCache->GetSize() << " bytes" << std::endl;
}
else
#endif
{
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.shaderProgram = shaderProgram;
pipelineDesc.renderPass = context->GetRenderPass();
#ifdef ENABLE_MULTISAMPLING
pipelineDesc.rasterizer.multiSampleEnabled = (contextDesc.samples > 1);
#endif
}
#ifdef ENABLE_CACHED_PSO
// Create and cache graphics PSO
pipeline = renderer->CreatePipelineState(pipelineDesc, &pipelineCache);
if (pipelineCache)
{
std::cout << "Pipeline cache created: " << pipelineCache->GetSize() << " bytes" << std::endl;
// Store PSO cache to file
std::ofstream file{ cacheFilename, std::ios::out | std::ios::binary };
file.write(
reinterpret_cast<const char*>(pipelineCache->GetData()),
static_cast<std::streamsize>(pipelineCache->GetSize())
);
}
#else
// Create graphics PSO
pipeline = renderer->CreatePipelineState(pipelineDesc);
#endif
}
// Create command buffer to submit subsequent graphics commands to the GPU
LLGL::CommandBuffer* commands = renderer->CreateCommandBuffer(LLGL::CommandBufferFlags::ImmediateSubmit);
#ifdef ENABLE_TIMER
auto timer = LLGL::Timer::Create();
auto start = std::chrono::system_clock::now();
#endif
// Enter main loop
while (window.ProcessEvents())
{
#ifdef ENABLE_TIMER
timer->MeasureTime();
auto end = std::chrono::system_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>(end - start).count() > 0)
{
std::cout << "Rendertime: " << timer->GetDeltaTime() << ", FPS: " << 1.0 / timer->GetDeltaTime() << '\n';
start = end;
}
#endif
// Begin recording commands
commands->Begin();
{
// Set viewport and scissor rectangle
commands->SetViewport(context->GetResolution());
// Set graphics pipeline
commands->SetPipelineState(*pipeline);
// Set vertex buffer
commands->SetVertexBuffer(*vertexBuffer);
// Set the render context as the initial render target
commands->BeginRenderPass(*context);
{
// Clear color buffer
commands->Clear(LLGL::ClearFlags::Color);
// Draw triangle with 3 vertices
commands->Draw(3, 0);
}
commands->EndRenderPass();
}
commands->End();
// Present the result on the screen
context->Present();
}
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
#ifdef _WIN32
system("pause");
#endif
}
return 0;
}