forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.cpp
325 lines (268 loc) · 11.8 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
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
/*
* Example.cpp (Example_Texturing)
*
* 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 <LLGL/Strings.h>
#include <DDSImageReader.h>
#include <stb/stb_image.h>
class Example_Texturing : public ExampleBase
{
LLGL::ShaderProgram* shaderProgram = nullptr;
LLGL::PipelineLayout* pipelineLayout = nullptr;
LLGL::PipelineState* pipeline = nullptr;
LLGL::Buffer* vertexBuffer = nullptr;
LLGL::Texture* colorMaps[2] = {};
LLGL::Sampler* sampler[5] = {};
LLGL::ResourceHeap* resourceHeap = {};
unsigned resourceIndex = 0;
std::array<std::string, 6> resourceLabels
{{
"format = BC1UNorm",
"format = RGBA8UNorm",
"mipMapLODBias = 3",
"minFilter = Nearest",
"addressModeU = MirrorOnce, addressModeV = Border",
"addressModeU = Mirror, addressModeV = Mirror",
}};
public:
Example_Texturing() :
ExampleBase { L"LLGL Example: Texturing" }
{
// Create all graphics objects
auto vertexFormat = CreateBuffers();
shaderProgram = LoadStandardShaderProgram({ vertexFormat });
CreatePipelines();
CreateTextures();
CreateSamplers();
CreateResourceHeap();
// Update resource labels
for (int i = 0; i < 2; ++i)
{
if (auto formatStr = LLGL::ToString(colorMaps[i]->GetDesc().format))
resourceLabels[i] = (std::string("format = ") + formatStr);
}
// Print some information on the standard output
std::cout << "press TAB KEY to switch between five different texture samplers" << std::endl;
std::cout << "texture attributes: " << resourceLabels[0] << "\r";
std::flush(std::cout);
}
LLGL::VertexFormat CreateBuffers()
{
// Specify vertex format
LLGL::VertexFormat vertexFormat;
vertexFormat.AppendAttribute({ "position", LLGL::Format::RG32Float });
vertexFormat.AppendAttribute({ "texCoord", LLGL::Format::RG32Float });
// Define vertex buffer data
struct Vertex
{
Gs::Vector2f position;
Gs::Vector2f texCoord;
};
std::vector<Vertex> vertices =
{
{ { -1, 1 }, { -2, -2 } },
{ { -1, -1 }, { -2, 2 } },
{ { 1, 1 }, { 2, -2 } },
{ { 1, -1 }, { 2, 2 } },
};
// Create vertex buffer
vertexBuffer = CreateVertexBuffer(vertices, vertexFormat);
return vertexFormat;
}
void CreatePipelines()
{
// Create pipeline layout
LLGL::PipelineLayoutDescriptor layoutDesc;
{
layoutDesc.bindings =
{
LLGL::BindingDescriptor{ LLGL::ResourceType::Sampler, 0, LLGL::StageFlags::FragmentStage, 0 },
LLGL::BindingDescriptor{ LLGL::ResourceType::Texture, LLGL::BindFlags::Sampled, LLGL::StageFlags::FragmentStage, (IsOpenGL() ? 0u : 1u) },
};
}
pipelineLayout = renderer->CreatePipelineLayout(layoutDesc);
// Create graphics pipeline
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.shaderProgram = shaderProgram;
pipelineDesc.pipelineLayout = pipelineLayout;
pipelineDesc.primitiveTopology = LLGL::PrimitiveTopology::TriangleStrip;
pipelineDesc.rasterizer.multiSampleEnabled = (GetSampleCount() > 1);
}
pipeline = renderer->CreatePipelineState(pipelineDesc);
}
void LoadUncompressedTexture(const std::string& filename)
{
// Load image data from file (using STBI library, see http://nothings.org/stb_image.h)
int texWidth = 0, texHeight = 0, texComponents = 0;
unsigned char* imageBuffer = stbi_load(filename.c_str(), &texWidth, &texHeight, &texComponents, 0);
if (!imageBuffer)
throw std::runtime_error("failed to load image from file: " + filename);
// Initialize source image descriptor to upload image data onto hardware texture
LLGL::SrcImageDescriptor imageDesc;
{
// Set image color format
imageDesc.format = (texComponents == 4 ? LLGL::ImageFormat::RGBA : LLGL::ImageFormat::RGB);
// Set image data type (unsigned char = 8-bit unsigned integer)
imageDesc.dataType = LLGL::DataType::UInt8;
// Set image buffer source for texture initial data
imageDesc.data = imageBuffer;
// Set image buffer size
imageDesc.dataSize = static_cast<std::size_t>(texWidth*texHeight*texComponents);
}
// Upload image data onto hardware texture and stop the time
timer->Start();
{
// Create texture
LLGL::TextureDescriptor texDesc;
{
// Texture type: 2D
texDesc.type = LLGL::TextureType::Texture2D;
// Texture hardware format: RGBA with normalized 8-bit unsigned char type
texDesc.format = LLGL::Format::BGRA8UNorm;//RGBA8UNorm; //BGRA8UNorm
// Texture size
texDesc.extent = { static_cast<std::uint32_t>(texWidth), static_cast<std::uint32_t>(texHeight), 1u };
// Generate all MIP-map levels for this texture
texDesc.miscFlags = LLGL::MiscFlags::GenerateMips;
}
colorMaps[1] = renderer->CreateTexture(texDesc, &imageDesc);
}
auto texCreationTime = static_cast<double>(timer->Stop()) / static_cast<double>(timer->GetFrequency());
std::cout << "texture creation time: " << (texCreationTime * 1000.0) << " ms" << std::endl;
// Release image data
stbi_image_free(imageBuffer);
}
void LoadCompressedTexture(const std::string& filename)
{
// Load DDS image
DDSImageReader imageReader;
imageReader.LoadFromFile(filename);
auto texDesc = imageReader.GetTextureDesc();
auto imageDesc = imageReader.GetImageDesc();
// Create texture with MIP-map level 0
imageDesc.dataSize = LLGL::GetMemoryFootprint(texDesc.format, texDesc.extent.width * texDesc.extent.height * texDesc.extent.depth);
colorMaps[0] = renderer->CreateTexture(texDesc, &imageDesc);
// Write MIP-map levels 1...N
const auto& formatDesc = LLGL::GetFormatAttribs(texDesc.format);
for (std::uint32_t mipLevel = 1; mipLevel < texDesc.mipLevels; ++mipLevel)
{
// Determine texture region for next MIP-map level
LLGL::TextureRegion region;
region.extent.width = std::max(texDesc.extent.width >> mipLevel, 1u);
region.extent.height = std::max(texDesc.extent.height >> mipLevel, 1u);
region.extent.depth = std::max(texDesc.extent.depth >> mipLevel, 1u);
region.subresource.baseMipLevel = mipLevel;
region.subresource.numMipLevels = 1;
// MIP-maps of block compression must be a multiple of the block size, so we cannot go smaller
if (region.extent.width >= formatDesc.blockWidth &&
region.extent.height >= formatDesc.blockHeight)
{
// Update image descriptor for subresource
std::size_t mipLevelDataSize = LLGL::GetMemoryFootprint(texDesc.format, region.extent.width * region.extent.height * region.extent.depth);
imageDesc.data = reinterpret_cast<const std::int8_t*>(imageDesc.data) + imageDesc.dataSize;
imageDesc.dataSize = mipLevelDataSize;
renderer->WriteTexture(*colorMaps[0], region, imageDesc);
}
}
}
void CreateTextures()
{
LoadCompressedTexture("../../Media/Textures/Crate-DXT1-MipMapped.dds");
LoadUncompressedTexture("../../Media/Textures/Crate.jpg");
}
void CreateSamplers()
{
// Create 1st sampler state with default settings
LLGL::SamplerDescriptor samplerDesc;
sampler[0] = renderer->CreateSampler(samplerDesc);
// Create 2nd sampler state with MIP-map bias
samplerDesc.mipMapLODBias = 3.0f;
sampler[1] = renderer->CreateSampler(samplerDesc);
// Create 3rd sampler state with nearest filtering
samplerDesc.minFilter = LLGL::SamplerFilter::Nearest;
sampler[2] = renderer->CreateSampler(samplerDesc);
// Create 4th sampler state with clamped texture wrap mode
samplerDesc.minFilter = LLGL::SamplerFilter::Linear;
samplerDesc.mipMapLODBias = 0.0f;
samplerDesc.addressModeU = LLGL::SamplerAddressMode::MirrorOnce;
samplerDesc.addressModeV = LLGL::SamplerAddressMode::Border;
sampler[3] = renderer->CreateSampler(samplerDesc);
// Create 5th sampler state with mirrored texture wrap mode
samplerDesc.addressModeU = LLGL::SamplerAddressMode::Mirror;
samplerDesc.addressModeV = LLGL::SamplerAddressMode::Mirror;
sampler[4] = renderer->CreateSampler(samplerDesc);
}
void CreateResourceHeap()
{
#if 0//TESTING
uint8_t imageData = 255;
LLGL::SrcImageDescriptor imageDesc;
{
imageDesc.format = LLGL::ImageFormat::R;
imageDesc.dataType = LLGL::DataType::UInt8;
imageDesc.data = &imageData;
imageDesc.dataSize = 1;
}
LLGL::TextureDescriptor texDesc;
{
texDesc.extent = { 1, 1, 1 };
texDesc.format = LLGL::Format::R8UInt;
}
auto texUint = renderer->CreateTexture(texDesc, &imageDesc);
texUint->SetName("texUint");
#endif
LLGL::ResourceHeapDescriptor resourceHeapDesc;
{
resourceHeapDesc.pipelineLayout = pipelineLayout;
resourceHeapDesc.resourceViews.reserve(6 * 2);
for (int i = 0; i < 6; ++i)
{
resourceHeapDesc.resourceViews.push_back(sampler[i > 0 ? i - 1 : 0]);
resourceHeapDesc.resourceViews.push_back(/*i == 0 ? texUint : */colorMaps[i == 0 ? 0 : 1]);
}
}
resourceHeap = renderer->CreateResourceHeap(resourceHeapDesc);
}
private:
void OnDrawFrame() override
{
// Examine user input
if (input->KeyDown(LLGL::Key::Tab))
{
if (input->KeyPressed(LLGL::Key::Shift))
resourceIndex = ((resourceIndex - 1) % 6 + 6) % 6;
else
resourceIndex = (resourceIndex + 1) % 6;
std::cout << "texture attributes: " << resourceLabels[resourceIndex] << std::string(30, ' ') << "\r";
std::flush(std::cout);
}
// Set render target
commands->Begin();
{
// Set vertex buffer
commands->SetVertexBuffer(*vertexBuffer);
commands->BeginRenderPass(*context);
{
// Clear color buffer
commands->Clear(LLGL::ClearFlags::Color);
// Set viewports
commands->SetViewport(context->GetVideoMode().resolution);
// Set graphics pipeline and vertex buffer
commands->SetPipelineState(*pipeline);
// Set graphics shader resources
commands->SetResourceHeap(*resourceHeap, resourceIndex);
// Draw fullscreen quad
commands->Draw(4, 0);
}
commands->EndRenderPass();
}
commands->End();
commandQueue->Submit(*commands);
// Present result on the screen
context->Present();
}
};
LLGL_IMPLEMENT_EXAMPLE(Example_Texturing);