-
Notifications
You must be signed in to change notification settings - Fork 158
/
HelloWorldSession.cpp
202 lines (170 loc) · 6.55 KB
/
HelloWorldSession.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
/*
* 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 "HelloWorldSession.h"
#include <igl/opengl/Device.h>
#include <igl/opengl/GLIncludes.h>
#include <igl/opengl/RenderCommandEncoder.h>
#include <regex>
namespace igl::shell {
static std::string getMetalShaderSource() {
return R"(
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
constant float2 pos[3] = {
float2(-0.6, -0.4),
float2( 0.6, -0.4),
float2( 0.0, 0.6)
};
constant float3 col[3] = {
float3(1.0, 0.0, 0.0),
float3(0.0, 1.0, 0.0),
float3(0.0, 0.0, 1.0)
};
struct VertexOut {
float4 position [[position]];
float3 uvw;
};
vertex VertexOut vertexShader(uint vid [[vertex_id]]) {
VertexOut out;
out.position = float4(pos[vid], 0.0, 1.0);
out.uvw = col[vid];
return out;
}
fragment float4 fragmentShader(
VertexOut in[[stage_in]]) {
float4 tex = float4(in.uvw,1.0);
return tex;
}
)";
}
static const char* getVulkanVertexShaderSource() {
return R"(
#version 460
layout (location=0) out vec3 color;
const vec2 pos[3] = vec2[3](
vec2(-0.6, -0.4),
vec2( 0.6, -0.4),
vec2( 0.0, 0.6)
);
const vec3 col[3] = vec3[3](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
void main() {
gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0);
color = col[gl_VertexIndex];
}
)";
}
static const char* getVulkanFragmentShaderSource() {
return R"(
#version 460
layout (location=0) in vec3 color;
layout (location=0) out vec4 out_FragColor;
void main() {
out_FragColor = vec4(color, 1.0);
}
)";
}
static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& device) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
return igl::ShaderStagesCreator::fromModuleStringInput(device,
getVulkanVertexShaderSource(),
"main",
"",
getVulkanFragmentShaderSource(),
"main",
"",
nullptr);
return nullptr;
// @fb-only
// @fb-only
// @fb-only
case igl::BackendType::Metal:
return igl::ShaderStagesCreator::fromLibraryStringInput(
device, getMetalShaderSource().c_str(), "vertexShader", "fragmentShader", "", nullptr);
case igl::BackendType::OpenGL:
#if IGL_BACKEND_OPENGL
auto glVersion =
static_cast<igl::opengl::Device&>(device).getContext().deviceFeatures().getGLVersion();
if (glVersion > igl::opengl::GLVersion::v2_1) {
auto codeVS1 = std::regex_replace(
getVulkanVertexShaderSource(), std::regex("gl_VertexIndex"), "gl_VertexID");
auto codeVS2 = std::regex_replace(codeVS1.c_str(), std::regex("460"), "410");
auto codeFS = std::regex_replace(getVulkanFragmentShaderSource(), std::regex("460"), "410");
return igl::ShaderStagesCreator::fromModuleStringInput(
device, codeVS2.c_str(), "main", "", codeFS.c_str(), "main", "", nullptr);
} else {
IGL_ASSERT_MSG(0, "This sample is incompatible with OpenGL 2.1");
return nullptr;
}
#else
return nullptr;
#endif // IGL_BACKEND_OPENGL
}
IGL_UNREACHABLE_RETURN(nullptr)
}
void HelloWorldSession::initialize() noexcept {
// Command queue: backed by different types of GPU HW queues
CommandQueueDesc desc{CommandQueueType::Graphics};
commandQueue_ = getPlatform().getDevice().createCommandQueue(desc, nullptr);
renderPass_.colorAttachments.resize(1);
renderPass_.colorAttachments[0] = igl::RenderPassDesc::ColorAttachmentDesc{};
renderPass_.colorAttachments[0].loadAction = LoadAction::Clear;
renderPass_.colorAttachments[0].storeAction = StoreAction::Store;
renderPass_.colorAttachments[0].clearColor = getPlatform().getDevice().backendDebugColor();
renderPass_.depthAttachment.loadAction = LoadAction::DontCare;
}
void HelloWorldSession::update(igl::SurfaceTextures surfaceTextures) noexcept {
FramebufferDesc framebufferDesc;
framebufferDesc.colorAttachments[0].texture = surfaceTextures.color;
const auto dimensions = surfaceTextures.color->getDimensions();
framebuffer_ = getPlatform().getDevice().createFramebuffer(framebufferDesc, nullptr);
IGL_ASSERT(framebuffer_);
if (!renderPipelineState_Triangle_) {
RenderPipelineDesc desc;
desc.targetDesc.colorAttachments.resize(1);
if (framebuffer_->getColorAttachment(0)) {
desc.targetDesc.colorAttachments[0].textureFormat =
framebuffer_->getColorAttachment(0)->getProperties().format;
}
if (framebuffer_->getDepthAttachment()) {
desc.targetDesc.depthAttachmentFormat =
framebuffer_->getDepthAttachment()->getProperties().format;
}
desc.shaderStages = getShaderStagesForBackend(getPlatform().getDevice());
renderPipelineState_Triangle_ = getPlatform().getDevice().createRenderPipeline(desc, nullptr);
IGL_ASSERT(renderPipelineState_Triangle_);
}
framebuffer_->updateDrawable(surfaceTextures.color);
// Command buffers (1-N per thread): create, submit and forget
CommandBufferDesc cbDesc;
std::shared_ptr<ICommandBuffer> buffer = commandQueue_->createCommandBuffer(cbDesc, nullptr);
const igl::Viewport viewport = {
0.0f, 0.0f, (float)dimensions.width, (float)dimensions.height, 0.0f, +1.0f};
const igl::ScissorRect scissor = {0, 0, (uint32_t)dimensions.width, (uint32_t)dimensions.height};
// This will clear the framebuffer
auto commands = buffer->createRenderCommandEncoder(renderPass_, framebuffer_);
commands->bindRenderPipelineState(renderPipelineState_Triangle_);
commands->bindViewport(viewport);
commands->bindScissorRect(scissor);
commands->pushDebugGroupLabel("Render Triangle", igl::Color(1, 0, 0));
commands->draw(3);
commands->popDebugGroupLabel();
commands->endEncoding();
buffer->present(surfaceTextures.color);
commandQueue_->submit(*buffer);
RenderSession::update(surfaceTextures);
}
} // namespace igl::shell