Skip to content

Commit 0faebd0

Browse files
committed
Simplify shadertoy shaders and buffers
- Removed the vertex and the index buffers - Removed their respective descriptors - Use single large triangle for covering quad like in https://www.saschawillems.de/blog/2016/08/13/vulkan-tutorial-on-rendering-a-fullscreen-quad-without-buffers/ - Change mouse behavior to react only if mouse is pressed
1 parent a50082b commit 0faebd0

File tree

1 file changed

+16
-43
lines changed

1 file changed

+16
-43
lines changed

examples/app/vsgshadertoy/vsgshadertoy.cpp

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,20 @@ layout(set = 0, binding = 0) uniform UBO {
3838
int iFrame;
3939
} ubo;
4040
41-
layout(location = 0) in vec2 inVertex;
42-
layout(location = 1) in vec2 inTexture;
43-
4441
layout(location = 0) out vec2 fragCoord;
4542
4643
out gl_PerVertex{ vec4 gl_Position; };
4744
4845
void main()
4946
{
50-
vec4 vertex = vec4(inVertex, 0, 1.0);
51-
52-
gl_Position = vec4(inVertex.x, -inVertex.y, 0.5, 1.0);
53-
54-
fragCoord = vec2(inTexture.x * ubo.iResolution.x,
55-
(1-inTexture.y) * ubo.iResolution.y);
47+
// fragCord is from (0→w,0→h)
48+
fragCoord = vec2((gl_VertexIndex << 1) & 2,
49+
(gl_VertexIndex & 2)) * ubo.iResolution;
50+
51+
// gl_Position is from (-1→1,-1→1)
52+
gl_Position = vec4(fragCoord.x/ubo.iResolution.x * 2.0 - 1.0,
53+
(1.0-fragCoord.y/ubo.iResolution.y) * 2.0 - 1.0,
54+
0.5, 1.0);
5655
}
5756
)";
5857

@@ -141,23 +140,13 @@ vsg::ref_ptr<vsg::Node> createToyNode(const std::string& toyShader,
141140

142141
// set up graphics pipeline
143142
vsg::DescriptorSetLayoutBindings descriptorBindings{
144-
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT|VK_SHADER_STAGE_FRAGMENT_BIT, nullptr} // { binding, descriptorType, descriptorCount, stageFlags, pImmutableSamplers}
143+
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT|VK_SHADER_STAGE_FRAGMENT_BIT, nullptr} // { binding, descriptorType, descriptorCount, stageFlags, pImmutableSamplers}
145144
};
146145

147146
auto descriptorSetLayout = vsg::DescriptorSetLayout::create(descriptorBindings);
148147

149-
vsg::VertexInputState::Bindings vertexBindingsDescriptions{
150-
VkVertexInputBindingDescription{0, sizeof(vsg::vec2), VK_VERTEX_INPUT_RATE_VERTEX}, // vertex data
151-
VkVertexInputBindingDescription{1, sizeof(vsg::vec2), VK_VERTEX_INPUT_RATE_VERTEX} // tex coord data
152-
};
153-
154-
vsg::VertexInputState::Attributes vertexAttributeDescriptions{
155-
VkVertexInputAttributeDescription{0, 0, VK_FORMAT_R32G32_SFLOAT, 0}, // vertex data
156-
VkVertexInputAttributeDescription{1, 1, VK_FORMAT_R32G32_SFLOAT, 0} // tex coord data
157-
};
158-
159148
vsg::GraphicsPipelineStates pipelineStates{
160-
vsg::VertexInputState::create(vertexBindingsDescriptions, vertexAttributeDescriptions),
149+
vsg::VertexInputState::create(), // No vertices for shader toy
161150
vsg::InputAssemblyState::create(),
162151
vsg::RasterizationState::create(),
163152
vsg::MultisampleState::create(),
@@ -178,28 +167,9 @@ vsg::ref_ptr<vsg::Node> createToyNode(const std::string& toyShader,
178167
node->add(bindGraphicsPipeline);
179168
node->add(bindDescriptorSet);
180169

181-
// set up vertex and index arrays
182-
auto vertices = vsg::vec2Array::create(
183-
{{-1.0f, -1.0f},
184-
{1.0f, -1.0f},
185-
{1.0f, 1.0f},
186-
{-1.0f, 1.0f}}); // VK_FORMAT_R32G32_SFLOAT, VK_VERTEX_INPUT_RATE_INSTANCE, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_SHARING_MODE_EXCLUSIVE
187-
188-
auto texcoords = vsg::vec2Array::create(
189-
{{0.0f, 1.0f},
190-
{1.0f, 1.0f},
191-
{1.0f, 0.0f},
192-
{0.0f, 0.0f}}); // VK_FORMAT_R32G32_SFLOAT, VK_VERTEX_INPUT_RATE_VERTEX, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_SHARING_MODE_EXCLUSIVE
193-
194-
auto indices = vsg::ushortArray::create(
195-
{0, 1, 2,
196-
2, 3, 0}); // VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_SHARING_MODE_EXCLUSIVE
197-
198170
// setup geometry
199171
auto drawCommands = vsg::Commands::create();
200-
drawCommands->addChild(vsg::BindVertexBuffers::create(0, vsg::DataList{vertices, texcoords}));
201-
drawCommands->addChild(vsg::BindIndexBuffer::create(indices));
202-
drawCommands->addChild(vsg::DrawIndexed::create(6, 1, 0, 0, 0));
172+
drawCommands->addChild(vsg::Draw::create(3, 1, 0, 0)); // Draw without vertices, as they are generated from gl_VertexIndex
203173

204174
// add drawCommands to transform
205175
node->addChild(drawCommands);
@@ -213,10 +183,11 @@ class MouseHandler : public vsg::Inherit<vsg::Visitor, MouseHandler>
213183
void apply(vsg::PointerEvent& pointerEvent) override
214184
{
215185
lastPointerEvent = &pointerEvent;
186+
isPressed = pointerEvent.mask != vsg::BUTTON_MASK_OFF;
216187
}
217188

218-
219189
vsg::ref_ptr<vsg::PointerEvent> lastPointerEvent;
190+
bool isPressed = false;
220191
};
221192

222193
int main(int argc, char** argv)
@@ -305,7 +276,9 @@ int main(int argc, char** argv)
305276
toyUniform->value().iResolution = {(int)extent.width, (int)extent.height};
306277
toyUniform->value().iTime = std::chrono::duration<float>(std::chrono::steady_clock::now()-t0).count();
307278
toyUniform->value().iFrame += 1;
308-
toyUniform->value().iMouse = mouseHandler->lastPointerEvent ? vsg::vec2(mouseHandler->lastPointerEvent->x, extent.height-mouseHandler->lastPointerEvent->y) : vsg::vec2(0,0);
279+
280+
if (mouseHandler->isPressed)
281+
toyUniform->value().iMouse = mouseHandler->lastPointerEvent ? vsg::vec2(mouseHandler->lastPointerEvent->x, extent.height-mouseHandler->lastPointerEvent->y) : vsg::vec2(0,0);
309282

310283
toyUniform->dirty();
311284

0 commit comments

Comments
 (0)