Repository navigation
Expand file tree
/
Copy pathTQMultiRenderPassSession.cpp
More file actions
506 lines (448 loc) · 17.1 KB
/
Copy pathTQMultiRenderPassSession.cpp
File metadata and controls
506 lines (448 loc) · 17.1 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @fb-only
#include <shell/renderSessions/TQMultiRenderPassSession.h>
#include <IGLU/simdtypes/SimdTypes.h>
#include <shell/shared/renderSession/ShellParams.h>
#include <igl/NameHandle.h>
#include <igl/ShaderCreator.h>
namespace igl::shell {
namespace {
struct VertexPosUv {
iglu::simdtypes::float3 position; // SIMD 128b aligned
iglu::simdtypes::float2 uv; // SIMD 128b aligned
};
// clang-format off
const VertexPosUv kVertexData0[] = {
{.position = {-1.0f, 1.0f, 0.0f}, .uv = {0.0f, 1.0f}},
{.position = { 1.0f, 1.0f, 0.0f}, .uv = {1.0f, 1.0f}},
{.position = {-1.0f, -1.0f, 0.0f}, .uv = {0.0f, 0.0f}},
{.position = { 1.0f, -1.0f, 0.0f}, .uv = {1.0f, 0.0f}},
};
const VertexPosUv kVertexData1[] = {
{.position = {-0.8f, 0.8f, 0.0f}, .uv = {0.0f, 1.0f}},
{.position = { 0.8f, 0.8f, 0.0f}, .uv = {1.0f, 1.0f}},
{.position = {-0.8f, -0.8f, 0.0f}, .uv = {0.0f, 0.0f}},
{.position = { 0.8f, -0.8f, 0.0f}, .uv = {1.0f, 0.0f}},
};
constexpr uint16_t kIndexData[] = {0, 1, 2, 1, 3, 2};
// clang-format on
std::string getMetalShaderSource() {
return R"(
using namespace metal;
typedef struct { float3 color; } UniformBlock;
typedef struct {
float3 position [[attribute(0)]];
float2 uv [[attribute(1)]];
} VertexIn;
typedef struct {
float4 position [[position]];
float2 uv;
} VertexOut;
vertex VertexOut vertexShader(
uint vid [[vertex_id]], constant VertexIn * vertices [[buffer(0)]]) {
VertexOut out;
out.position = float4(vertices[vid].position, 1.0);
out.uv = vertices[vid].uv;
return out;
}
fragment float4 fragmentShader(
VertexOut IN [[stage_in]],
texture2d<float> diffuseTex [[texture(0)]],
sampler linearSampler [[sampler(0)]],
constant UniformBlock * color [[buffer(0)]]) {
float4 tex = diffuseTex.sample(linearSampler, IN.uv);
return float4(color->color.r, color->color.g, color->color.b, 1.0) *
tex;
}
)";
}
std::string getOpenGLVertexShaderSource() {
return R"(#version 100
attribute vec3 position;
attribute vec2 uv_in;
varying vec2 uv;
void main() {
gl_Position = vec4(position, 1.0);
uv = uv_in; // position.xy * 0.5 + 0.5;
})";
}
std::string getOpenGLFragmentShaderSource() {
return R"(#version 100
precision highp float;
uniform vec3 color;
uniform sampler2D inputImage;
varying vec2 uv;
void main() {
gl_FragColor =
vec4(color, 1.0) * texture2D(inputImage, uv);
})";
}
std::string getD3D12VertexShaderSource() {
return R"(
struct VertexIn {
float3 position : POSITION;
float2 uv : TEXCOORD0;
};
struct VertexOut {
float4 position : SV_Position;
float2 uv : TEXCOORD0;
};
VertexOut main(VertexIn IN) {
VertexOut OUT;
OUT.position = float4(IN.position, 1.0);
OUT.uv = IN.uv;
return OUT;
}
)";
}
std::string getD3D12FragmentShaderSource() {
return R"(
cbuffer UniformBlock : register(b0) {
float3 color;
};
Texture2D inputImage : register(t0);
SamplerState linearSampler : register(s0);
struct VertexOut {
float4 position : SV_Position;
float2 uv : TEXCOORD0;
};
float4 main(VertexOut IN) : SV_Target {
float4 tex = inputImage.Sample(linearSampler, IN.uv);
return float4(color.r, color.g, color.b, 1.0) * tex;
}
)";
}
// Bind convention: texture unit 0 at @group(0) @binding(0), its sampler at @binding(1), buffer 0
// at @group(1) @binding(0).
const char* getWgslShaderSource() {
return R"(
struct UniformBlock {
color : vec3f,
};
@group(1) @binding(0) var<uniform> ub : UniformBlock;
@group(0) @binding(0) var diffuseTex : texture_2d<f32>;
@group(0) @binding(1) var linearSampler : sampler;
struct VertexOut {
@builtin(position) position : vec4f,
@location(0) uv : vec2f,
};
@vertex
fn vertexShader(@location(0) position : vec3f, @location(1) uv : vec2f) -> VertexOut {
return VertexOut(vec4f(position, 1.0), uv);
}
@fragment
fn fragmentShader(v : VertexOut) -> @location(0) vec4f {
return vec4f(ub.color, 1.0) * textureSample(diffuseTex, linearSampler, v.uv);
}
)";
}
std::unique_ptr<IShaderStages> getShaderStagesForBackend(IDevice& device) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_DEBUG_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
IGL_LOG_INFO("TQMultiRenderPassSession: no Vulkan shaders; skipping\n");
return nullptr;
case igl::BackendType::Custom:
IGL_DEBUG_ABORT("IGLSamples not set up for Custom");
return nullptr;
// @fb-only
// @fb-only
// @fb-only
case igl::BackendType::WebGPU:
return igl::ShaderStagesCreator::fromLibraryStringInput(
device, getWgslShaderSource(), "vertexShader", "fragmentShader", "", nullptr);
case igl::BackendType::Metal:
return igl::ShaderStagesCreator::fromLibraryStringInput(
device, getMetalShaderSource().c_str(), "vertexShader", "fragmentShader", "", nullptr);
case igl::BackendType::OpenGL:
return igl::ShaderStagesCreator::fromModuleStringInput(device,
getOpenGLVertexShaderSource().c_str(),
"main",
"",
getOpenGLFragmentShaderSource().c_str(),
"main",
"",
nullptr);
case igl::BackendType::D3D12:
return igl::ShaderStagesCreator::fromModuleStringInput(device,
getD3D12VertexShaderSource().c_str(),
"main",
"",
getD3D12FragmentShaderSource().c_str(),
"main",
"",
nullptr);
}
IGL_UNREACHABLE_RETURN(nullptr)
}
void render(std::shared_ptr<ICommandBuffer>& buffer,
std::shared_ptr<IBuffer>& vertexBuffer,
std::shared_ptr<ITexture>& inputTexture,
std::shared_ptr<IRenderPipelineState>& pipelineState,
std::shared_ptr<IFramebuffer>& framebuffer,
RenderPassDesc& renderPass,
std::shared_ptr<ISamplerState>& samplerState,
std::shared_ptr<IBuffer>& ib,
size_t textureUnit,
BackendType& backend,
std::shared_ptr<IBuffer>& fragmentParamBuffer,
std::vector<UniformDesc>& fragmentUniformDescriptors,
FragmentFormat fragmentParameters) {
// Submit commands
const std::shared_ptr<IRenderCommandEncoder> commands =
buffer->createRenderCommandEncoder(renderPass, framebuffer);
commands->bindRenderPipelineState(pipelineState);
if (backend != igl::BackendType::OpenGL) {
if (fragmentParamBuffer) {
commands->bindBuffer(0, fragmentParamBuffer.get());
}
} else {
// Bind non block uniforms
for (const auto& uniformDesc : fragmentUniformDescriptors) {
commands->bindUniform(uniformDesc, &fragmentParameters);
}
}
commands->bindTexture(textureUnit, BindTarget::kFragment, inputTexture.get());
commands->bindSamplerState(textureUnit, BindTarget::kFragment, samplerState.get());
commands->bindVertexBuffer(0, *vertexBuffer);
commands->bindIndexBuffer(*ib, IndexFormat::UInt16);
commands->drawIndexed(6);
commands->endEncoding();
}
} // namespace
// NOLINTNEXTLINE(bugprone-exception-escape)
void TQMultiRenderPassSession::initialize() noexcept {
auto& device = getPlatform().getDevice();
// Vertex buffer, Index buffer and Vertex Input
vb0_ = device.createBuffer(BufferDesc{.type = BufferDesc::BufferTypeBits::Vertex,
.data = kVertexData0,
.length = sizeof(kVertexData0)},
nullptr);
vb1_ = device.createBuffer(BufferDesc{.type = BufferDesc::BufferTypeBits::Vertex,
.data = kVertexData1,
.length = sizeof(kVertexData1)},
nullptr);
ib0_ = device.createBuffer(BufferDesc{.type = BufferDesc::BufferTypeBits::Index,
.data = kIndexData,
.length = sizeof(kIndexData)},
nullptr);
const VertexInputStateDesc inputDesc = {
.numAttributes = 2,
.attributes =
{
{
.bufferIndex = 0,
.format = VertexAttributeFormat::Float3,
.offset = offsetof(VertexPosUv, position),
.name = "position",
.location = 0,
},
{
.bufferIndex = 0,
.format = VertexAttributeFormat::Float2,
.offset = offsetof(VertexPosUv, uv),
.name = "uv_in",
.location = 1,
},
},
.numInputBindings = 1,
.inputBindings =
{
{
.stride = sizeof(VertexPosUv),
},
},
};
vertexInputState_ = device.createVertexInputState(inputDesc, nullptr);
// Sampler & Texture
samplerState_ = device.createSamplerState(
SamplerStateDesc{
.minFilter = SamplerMinMagFilter::Linear,
.magFilter = SamplerMinMagFilter::Linear,
.debugName = "Sampler: linear",
},
nullptr);
tex0_ = getPlatform().loadTexture("igl.png");
shaderStages_ = getShaderStagesForBackend(device);
// Command queue
commandQueue_ = device.createCommandQueue({}, nullptr);
renderPass0_ = {
.colorAttachments =
{
{
.loadAction = LoadAction::Clear,
.storeAction = StoreAction::Store,
.clearColor = getPreferredClearColor(),
},
},
.depthAttachment = {.loadAction = LoadAction::Clear, .clearDepth = 1.0},
};
renderPass1_ = {
.colorAttachments =
{
{
.loadAction = LoadAction::Clear,
.storeAction = StoreAction::Store,
.clearColor = getPreferredClearColor(),
},
},
.depthAttachment = {.loadAction = LoadAction::Clear, .clearDepth = 1.0},
};
// init uniforms
fragmentParameters_ = FragmentFormat{.color = {1.0f, 1.0f, 1.0f}};
fragmentParamBuffer_ = device.createBuffer(BufferDesc{.type = BufferDesc::BufferTypeBits::Uniform,
.data = &fragmentParameters_,
.length = sizeof(fragmentParameters_),
.storage = ResourceStorage::Shared},
nullptr);
}
// NOLINTNEXTLINE(bugprone-exception-escape)
void TQMultiRenderPassSession::update(SurfaceTextures surfaceTextures) noexcept {
// Per IGL guidelines, surfaceTextures.color may be null on some platforms
// before the surface is ready (e.g., during window resize on Android/iOS).
if (!surfaceTextures.color || !shaderStages_) {
return;
}
Result ret;
if (framebuffer0_ == nullptr) {
const auto dimensions = surfaceTextures.color->getDimensions();
const TextureDesc desc1 =
igl::TextureDesc::new2D(igl::TextureFormat::RGBA_UNorm8,
dimensions.width,
dimensions.height,
igl::TextureDesc::TextureUsageBits::Sampled |
igl::TextureDesc::TextureUsageBits::Attachment);
tex1_ = getPlatform().getDevice().createTexture(desc1, nullptr);
framebuffer0_ = getPlatform().getDevice().createFramebuffer(
FramebufferDesc{
.colorAttachments = {{.texture = tex1_}},
.depthAttachment =
{
.texture = getPlatform().getDevice().createTexture(
TextureDesc{
.width = dimensions.width,
.height = dimensions.height,
.usage = igl::TextureDesc::TextureUsageBits::Attachment,
.type = TextureType::TwoD,
.format = igl::TextureFormat::Z_UNorm24,
.storage = igl::ResourceStorage::Private,
},
&ret),
},
},
&ret);
IGL_DEBUG_ASSERT(ret.isOk());
IGL_DEBUG_ASSERT(framebuffer0_ != nullptr);
}
if (framebuffer1_ == nullptr) {
framebuffer1_ = getPlatform().getDevice().createFramebuffer(
FramebufferDesc{
.colorAttachments = {{.texture = surfaceTextures.color}},
.depthAttachment = {.texture = surfaceTextures.depth},
},
&ret);
IGL_DEBUG_ASSERT(ret.isOk());
IGL_DEBUG_ASSERT(framebuffer1_ != nullptr);
} else {
framebuffer1_->updateDrawable(surfaceTextures);
}
const size_t textureUnit = 0;
// Graphics pipeline
if (pipelineState0_ == nullptr) {
pipelineState0_ = getPlatform().getDevice().createRenderPipeline(
RenderPipelineDesc{
.vertexInputState = vertexInputState_,
.shaderStages = shaderStages_,
.targetDesc =
{
.colorAttachments =
{
{
.textureFormat = tex1_->getProperties().format,
},
},
.depthAttachmentFormat =
framebuffer0_->getDepthAttachment()->getProperties().format,
},
.cullMode = igl::CullMode::Back,
.frontFaceWinding = igl::WindingMode::Clockwise,
.fragmentUnitSamplerMap = {{textureUnit, IGL_NAMEHANDLE("inputImage")}},
},
nullptr);
pipelineState1_ = getPlatform().getDevice().createRenderPipeline(
RenderPipelineDesc{
.vertexInputState = vertexInputState_,
.shaderStages = shaderStages_,
.targetDesc =
{
.colorAttachments =
{
{
.textureFormat =
framebuffer1_->getColorAttachment(0)->getProperties().format,
},
},
.depthAttachmentFormat =
framebuffer1_->getDepthAttachment()->getProperties().format,
},
.cullMode = igl::CullMode::Back,
.frontFaceWinding = igl::WindingMode::Clockwise,
.fragmentUnitSamplerMap = {{textureUnit, IGL_NAMEHANDLE("inputImage")}},
},
nullptr);
// Set up uniform descriptors
fragmentUniformDescriptors_.emplace_back();
}
// Command buffer
auto buffer = commandQueue_->createCommandBuffer({}, nullptr);
// Draw render pass 0
auto drawableSurface = framebuffer1_->getColorAttachment(0);
framebuffer1_->updateDrawable(drawableSurface);
if (!fragmentUniformDescriptors_.empty()) {
fragmentUniformDescriptors_.back().location =
pipelineState0_->getIndexByName("color", igl::ShaderStage::Fragment);
fragmentUniformDescriptors_.back().type = UniformType::Float3;
fragmentUniformDescriptors_.back().offset = offsetof(FragmentFormat, color);
}
BackendType backendType = getPlatform().getDevice().getBackendType();
// Draw render pass 0
render(buffer,
vb0_,
tex0_,
pipelineState0_,
framebuffer0_,
renderPass0_,
samplerState_,
ib0_,
textureUnit,
backendType,
fragmentParamBuffer_,
fragmentUniformDescriptors_,
fragmentParameters_);
// Draw render pass 1
render(buffer,
vb1_,
tex1_,
pipelineState1_,
framebuffer1_,
renderPass1_,
samplerState_,
ib0_,
textureUnit,
backendType,
fragmentParamBuffer_,
fragmentUniformDescriptors_,
fragmentParameters_);
if (shellParams().shouldPresent) {
buffer->present(drawableSurface);
}
commandQueue_->submit(*buffer);
}
} // namespace igl::shell