-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04-vertex-transform.cpp
More file actions
164 lines (149 loc) · 5.55 KB
/
Copy path04-vertex-transform.cpp
File metadata and controls
164 lines (149 loc) · 5.55 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
#include "../framework/vulkanApp.h"
#include "../framework/utilities.h"
class VertexTransformApp : public VulkanApp
{
// OpenGL uses right-handed coordinate system, whilst Direct3D and RenderMan use left-handed
// https://www.evl.uic.edu/ralph/508S98/coordinates.html
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb204853(v=vs.85).aspx
constexpr static bool rhs = true;
struct DescriptorSetTable
{
magma::descriptor::UniformBuffer worldViewProj = 0;
} setTable;
std::unique_ptr<magma::VertexBuffer> vertexBuffer;
std::unique_ptr<magma::VertexBuffer> colorBuffer;
std::unique_ptr<magma::UniformBuffer<rapid::matrix>> uniformBuffer;
std::unique_ptr<magma::DescriptorSet> descriptorSet;
std::unique_ptr<magma::GraphicsPipeline> graphicsPipeline;
rapid::matrix viewProj;
public:
VertexTransformApp(const AppEntry& entry):
VulkanApp(entry, TEXT("04 - Vertex transform"), 512, 512)
{
initialize();
setupView();
createVertexBuffers();
createUniformBuffer();
setupDescriptorSet();
setupPipeline();
for (uint32_t i = 0; i < (uint32_t)commandBuffers.size(); ++i)
recordCommandBuffer(i);
timer->run();
}
void render(uint32_t bufferIndex) override
{
updatePerspectiveTransform();
submitCommandBuffer(bufferIndex);
}
void onResize(uint32_t width, uint32_t height) override
{
VulkanApp::onResize(width, height);
setupView();
for (uint32_t i = 0; i < (uint32_t)commandBuffers.size(); ++i)
recordCommandBuffer(i);
}
void setupView()
{
const rapid::vector3 eye(0.f, 0.f, 2.3f);
const rapid::vector3 center(0.f);
const rapid::vector3 up(0.f, 1.f, 0.f);
constexpr float fov = rapid::radians(45.f);
const float aspect = width/(float)height;
constexpr float zn = 0.1f, zf = 100.f;
rapid::matrix view, proj;
if (rhs)
{
view = rapid::lookAtRH(eye, center, up);
proj = rapid::perspectiveFovRH(fov, aspect, zn, zf);
}
else
{
view = rapid::lookAtLH(eye, center, up);
proj = rapid::perspectiveFovLH(fov, aspect, zn, zf);
}
viewProj = view * proj;
}
void updatePerspectiveTransform()
{
constexpr float speed = 0.05f;
const float step = timer->millisecondsElapsed() * speed;
static float angle = 0.f;
angle += rhs ? step : -step; // Preserve direction
const rapid::matrix world = rapid::rotationY(rapid::radians(angle));
magma::map(uniformBuffer,
[this, &world](auto *worldViewProj)
{
*worldViewProj = world * viewProj;
});
}
void createVertexBuffers()
{
// Take into account that unlike OpenGL, Vulkan Y axis points down the screen
const magma::Float2 vertices[] = {
{ 0.0f,-0.3f}, // top
{-0.6f, 0.3f}, // left
{ 0.6f, 0.3f} // right
};
const magma::UByteNorm4 colors[] = {
{255, 0, 0, 255},
{0, 255, 0, 255},
{0, 0, 255, 255}
};
vertexBuffer = utilities::makeVertexBuffer(vertices, cmdBufferCopy);
colorBuffer = utilities::makeVertexBuffer(colors, cmdBufferCopy);
}
void createUniformBuffer()
{
uniformBuffer = std::make_unique<magma::UniformBuffer<rapid::matrix>>(device);
}
void setupDescriptorSet()
{
setTable.worldViewProj = uniformBuffer;
descriptorSet = std::make_unique<magma::DescriptorSet>(descriptorPool,
setTable, VK_SHADER_STAGE_VERTEX_BIT,
nullptr, 0, shaderReflectionFactory, "transform");
}
void setupPipeline()
{
static constexpr magma::VertexInputStructure<magma::vt::Pos2fColor4ub, 2> twoStreamVertexInput(
{
MAGMA_VERTEX_STREAM_ATTRIBUTE(magma::vt::Pos2fColor4ub, pos, 0, 0),
MAGMA_VERTEX_STREAM_ATTRIBUTE(magma::vt::Pos2fColor4ub, color, 1, 1),
});
auto layout = std::make_unique<magma::PipelineLayout>(descriptorSet->getLayout());
graphicsPipeline = std::make_unique<GraphicsPipeline>(device,
"transform", "frontFace",
twoStreamVertexInput,
magma::renderstate::triangleList,
rhs ? magma::renderstate::fillCullNoneCcw
: magma::renderstate::fillCullNoneCw,
magma::renderstate::dontMultisample,
magma::renderstate::depthAlwaysDontWrite,
magma::renderstate::dontBlendRgb,
std::move(layout),
renderPass, 0,
pipelineCache);
}
void recordCommandBuffer(uint32_t index)
{
auto& cmdBuffer = commandBuffers[index];
cmdBuffer->begin();
{
cmdBuffer->beginRenderPass(renderPass, framebuffers[index], {magma::clear::gray});
{
cmdBuffer->setViewport(0, 0, width, height);
cmdBuffer->setScissor(0, 0, width, height);
cmdBuffer->bindDescriptorSet(graphicsPipeline, 0, descriptorSet);
cmdBuffer->bindPipeline(graphicsPipeline);
cmdBuffer->bindVertexBuffers(0, {vertexBuffer, colorBuffer}, {0, 0});
cmdBuffer->draw(3, 0);
}
cmdBuffer->endRenderPass();
}
cmdBuffer->end();
}
};
std::unique_ptr<IApplication> appFactory(const AppEntry& entry)
{
return std::unique_ptr<VertexTransformApp>(new VertexTransformApp(entry));
}