GlistEngine
Loading...
Searching...
No Matches
gVKDraw.h
Go to the documentation of this file.
1/*
2 * gVKDraw.h
3 *
4 * The Vulkan 2D draw path: a per-frame host-visible vertex ring plus the record
5 * helpers that the engine's 2D functions dispatch to under Vulkan. Every helper
6 * opens the render pass lazily (gvkEnsureRenderPass), so the clear colour set by
7 * the canvas earlier in the same draw() is what covers the screen.
8 *
9 * gvkCreateDrawResources / gvkDestroyDrawResources are declared in gVKContext.h
10 * (befriended there); the record helpers below need glm, so they live here.
11 */
12
13#pragma once
14
15#ifndef CORE_GVKDRAW_H
16#define CORE_GVKDRAW_H
17
18#include "gVKContext.h"
19// For gVKMeshPush, the 3D path's push constant block.
20#include "gVKUniform.h"
21
22#ifdef GVK_VULKAN
23
24#include <glm/glm.hpp>
25
26// The renderer's culling state, handed to a 3D draw the same way the depth state is.
27// Already in Vulkan terms: gVKRenderEngine translates the engine's GL-flavoured
28// culling API, so this header needs no GL constants.
29//
30// Inside the guard, like everything else here: a build without the Vulkan backend
31// never sees a Vulkan header, so a type named outside it does not exist at all.
32//
33// The front face is the opposite of the OpenGL one while the *screen* pass is
34// open: that pass draws through a negative-height viewport, which mirrors the y
35// axis and reverses the winding a triangle appears to have, so geometry OpenGL
36// calls counter-clockwise reaches this rasteriser as clockwise. An offscreen pass
37// uses a positive viewport and mirrors nothing, so there the OpenGL winding is
38// passed through unchanged. gVKContext::doesPassFlipY says which case is open, and
39// gVKRenderEngine::drawMesh3D is where the choice is made.
40struct gVKCullState {
41 VkCullModeFlags mode = VK_CULL_MODE_NONE;
42 VkFrontFace frontface = VK_FRONT_FACE_CLOCKWISE;
43};
44
45// How the points handed to gvkDrawColored2D are connected. These mirror the mesh
46// draw modes the 2D primitives use (gMesh::DRAWMODE_*), without this header having
47// to know the GL constants behind them. The fan / strip / loop forms are expanded
48// on the CPU, because the two colour pipelines are built as a triangle list and a
49// line list and Vulkan has no line loop topology at all.
50enum gvkDraw2DMode {
51 GVK_DRAW2D_TRIANGLES,
52 GVK_DRAW2D_TRIANGLESTRIP,
53 GVK_DRAW2D_TRIANGLEFAN,
54 GVK_DRAW2D_LINES,
55 GVK_DRAW2D_LINESTRIP,
56 GVK_DRAW2D_LINELOOP
57};
58
59// Records coloured 2D geometry. Colour components are 0..1; mvp is the full 2D
60// transform. points holds `count` positions connected as `mode` describes.
61// additive selects the pipeline that adds to the target instead of compositing
62// over it, for gRenderer::BLENDMODE_ADDITIVE. Blend factors are baked into a
63// Vulkan pipeline, so this is a choice of pipeline rather than a state to set.
64void gvkDrawColored2D(gVKContext& ctx, const glm::vec2* points, int count,
65 const glm::vec4& color, const glm::mat4& mvp, int mode = GVK_DRAW2D_TRIANGLES,
66 bool additive = false);
67
68// Records a textured unit quad (two triangles) sampled through the given combined
69// image sampler descriptor set. tint components are 0..1. uvOffset / uvScale place
70// the sampled region inside the texture: the default covers all of it, and a
71// sub-part draw narrows it to the requested rectangle. maskSet is the descriptor
72// set of an alpha mask whose inverted red channel scales the alpha, or
73// VK_NULL_HANDLE to draw unmasked.
74void gvkDrawTextured2D(gVKContext& ctx, VkDescriptorSet textureSet, VkDescriptorSet maskSet,
75 const glm::vec4& tint, const glm::mat4& mvp,
76 const glm::vec2& uvOffset = glm::vec2(0.0f), const glm::vec2& uvScale = glm::vec2(1.0f),
77 bool additive = false);
78
79// Redraws a single-sampled copy of the screen over the whole of the reopened pass,
80// with blending off so it replaces rather than composites. This is how a
81// multisampled frame gets its picture back after a render target was bound half way
82// through: the multisample image is transient and keeps nothing between passes, so
83// the resolved image is copied aside and put back through here. Recorded outside the
84// 2D batch, because it has to come before every draw in the pass.
85bool gvkDrawScreenRestore(gVKContext& ctx, VkDescriptorSet textureSet);
86
87// Records an expanded textured triangle list. xyuv has four floats per vertex:
88// position then texture coordinate.
89void gvkDrawTexturedTriangles2D(gVKContext& ctx, VkDescriptorSet textureSet,
90 const glm::vec4& tint, const glm::mat4& mvp, const float* xyuv, int vertexCount,
91 bool additive = false);
92
93// The 2D calls above do not record a draw of their own. Each appends its vertices
94// to an open batch, and the batch becomes a single vkCmdDraw here - a frame of a
95// couple of hundred sprites and glyphs collapses into a handful of draws whenever
96// consecutive ones share a texture and a blend mode.
97//
98// Order is preserved by ending the batch rather than reordering around it, so
99// anything that records into the same pass and must appear after the 2D issued
100// before it has to call this first. The 3D paths in this file already do; a new
101// one that records directly into the command buffer has to as well, and so does
102// any code that ends a render pass while the frame is still going.
103void gvkFlush2DBatch(gVKContext& ctx);
104
105/*
106 * Draws a screen-covering quad through whatever user shader is bound, which is
107 * how a post-process effect reaches the Vulkan backend: gPostProcessManager binds
108 * an effect's shader, points it at the previous pass's texture and asks for one
109 * quad covering the target.
110 *
111 * There is no pipeline of the engine's own behind this - the effect's own shader
112 * is the pipeline - so it does nothing and reports false when no user shader is
113 * bound, or when the bound one does not read the two inputs this path supplies.
114 */
115bool gvkDrawFullscreenQuad(gVKContext& ctx);
116
117// Resolves the current frame slot's G-buffer into the screen render pass through
118// the engine-owned deferred lighting pipeline.
119bool gvkDrawDeferredLighting(gVKContext& ctx, VkDescriptorSet gbufferSet,
120 VkDescriptorSet shadowSet);
121
122// Abandons the open batch without recording it, for the frame boundary: the range
123// it names is about to be rewound underneath it.
124void gvkReset2DBatch();
125
126// vertexOffset is where this mesh's vertices start inside the bound buffer. Zero
127// for a mesh uploaded once; a mesh whose vertices the CPU rewrites is given a
128// slice of the frame's arena instead, so that each of its draws reads the data
129// it was recorded with rather than whatever the last upload of the frame left.
130// Records one 3D mesh out of buffers that already live on the device, unlike the
131// 2D calls above, which copy their geometry into a per-frame ring first. A mesh
132// uploads once through gVKMeshBuffer and is then drawn straight from there.
133//
134// indexBuffer may be VK_NULL_HANDLE, in which case count is a vertex count and the
135// draw is non-indexed; otherwise count is an index count and indexType says how
136// wide those indices are - the caller passes it because gIndex is 16 bit on Android
137// and 32 bit elsewhere, and this file has no reason to depend on that header.
138//
139// push carries the model matrix and the material; the camera and the lights come
140// from the frame's scene descriptor set, which this binds.
141//
142// depthTest / depthTestAlways mirror the renderer's current depth state and are set
143// on the command buffer rather than baked into the pipeline, because the engine lets
144// an app toggle depth testing between draws. lines selects the wireframe pipeline.
145// materialSet holds the diffuse, specular and normal maps as bindings 0, 1 and 2 of
146// set 1, and shadowSet is set 2. Neither may be VK_NULL_HANDLE: the shader declares
147// every sampler, so each binding needs a valid descriptor even for a mesh that uses
148// no maps - the caller fills the unused ones with a 1x1 white texture.
149// topology has to belong to the class the chosen pipeline was built for: a triangle
150// one when lines is false, a line one when it is true.
151// instanceBuffer holds one model matrix per instance and is bound as binding 1. It
152// is required even for a single draw, because the shader always reads from it - the
153// caller passes a one-element identity buffer when the draw is not instanced.
154// cutout says whether this draw's diffuse map can produce a texel the shader's
155// alpha test would discard. False routes it through the pipeline with the discard
156// compiled out, which is what lets a tile based GPU reject its fragments on depth
157// before shading them; it must only be false where the caller knows that for
158// certain, because a cutout material drawn through it would render its holes
159// opaque. See GVK_CUTOUT in color_frag.glsl.
160void gvkDrawMesh3D(gVKContext& ctx, VkBuffer vertexBuffer, VkDeviceSize vertexOffset,
161 VkBuffer indexBuffer, int count,
162 VkIndexType indexType, const gVKMeshPush& push,
163 VkDescriptorSet materialSet, VkDescriptorSet shadowSet,
164 VkBuffer instanceBuffer, VkDeviceSize instanceOffset, int instanceCount,
165 VkPrimitiveTopology topology, bool depthTest, bool depthTestAlways, bool lines,
166 const gVKCullState& culling, bool blending, bool cutout);
167
168// Records one mesh into the shadow map through the depth-only pipeline. diffuseSet
169// is the caster's diffuse map, bound as set 0 so a cutout material can discard the
170// holes it punches; it must be a real descriptor even for an opaque mesh, where the
171// 1x1 white texture stands in and push.misc.x says not to sample it. Only valid
172// while the shadow render pass is open.
173void gvkDrawShadowCaster(gVKContext& ctx, VkBuffer vertexBuffer, VkDeviceSize vertexOffset,
174 VkBuffer indexBuffer,
175 int count, VkIndexType indexType, const gVKShadowPush& push,
176 VkDescriptorSet diffuseSet,
177 VkBuffer instanceBuffer, VkDeviceSize instanceOffset, int instanceCount, VkPrimitiveTopology topology);
178
179// Records one 3D mesh through the PBR pipeline. materialSet holds all five maps in
180// a single descriptor set (bindings 0..4), unlike the non-PBR path where each map
181// is its own set: Vulkan only guarantees four bound sets, and scene plus five
182// textures would need six. shadowSet is the depth map from the light's point of
183// view and goes in as set 2, per frame rather than per material. There is no line
184// variant - a wireframe PBR draw has no meaning that the OpenGL path offers either.
185void gvkDrawMesh3DPbr(gVKContext& ctx, VkBuffer vertexBuffer, VkDeviceSize vertexOffset,
186 VkBuffer indexBuffer, int count,
187 VkIndexType indexType, const gVKPbrPush& push, VkDescriptorSet materialSet,
188 VkDescriptorSet shadowSet,
189 VkBuffer instanceBuffer, VkDeviceSize instanceOffset, int instanceCount,
190 VkPrimitiveTopology topology, bool depthTest, bool depthTestAlways,
191 const gVKCullState& culling, bool blending);
192
193// Records one face of the skybox: a quad list in world space, sampled from a plain
194// 2D texture rather than a cube map. xyzuv holds five floats per vertex, three of
195// position and two of texture coordinate, and goes through the per-frame dynamic
196// ring because the quads are rebuilt around the camera every frame.
197//
198// depthCompare is the renderer's current depth function, so gSkybox can ask for the
199// same EQUAL test the OpenGL path uses to keep the sky behind everything else.
200void gvkDrawSkyboxFace(gVKContext& ctx, VkDescriptorSet faceSet, const float* xyzuv,
201 int vertexCount, const glm::mat4& viewProjection, VkCompareOp depthCompare);
202
203#endif /* GVK_VULKAN */
204
205#endif /* CORE_GVKDRAW_H */