GlistEngine
Loading...
Searching...
No Matches
gVKUniform.h
Go to the documentation of this file.
1/*
2 * gVKUniform.h
3 *
4 * The per-frame uniform buffer of the Vulkan 3D path: camera matrices and the
5 * scene's lights, in the std140 layout the mesh3d shaders declare.
6 *
7 * There is one buffer per frame in flight rather than a single shared one. The CPU
8 * writes next frame's values while the GPU may still be reading the previous
9 * frame's, so a single buffer would be rewritten underneath work already submitted.
10 * Each buffer stays permanently mapped: the data is rewritten every frame, and
11 * mapping and unmapping around each write would cost more than the memory saved.
12 *
13 * Created by: Veysel Burak Eroglu.
14 */
15
16#pragma once
17
18#ifndef CORE_GVKUNIFORM_H
19#define CORE_GVKUNIFORM_H
20
21#include "gVKContext.h"
22
23#ifdef GVK_VULKAN
24
25#include <glm/glm.hpp>
26
27// How many lights the shader's array holds. Mirrors GLIST_MAX_LIGHTS in
28// gRenderer.h; the static_assert in gVKUniform.cpp keeps the two from drifting.
29inline constexpr int GVK_MAX_LIGHTS = 8;
30
31/*
32 * One light, laid out to match the Light struct in color_frag.glsl under std140. The
33 * alignas values are what produce that layout: a vec3 aligns to 16 bytes even
34 * though it occupies 12, which is the rule that trips up hand-written UBO structs.
35 *
36 * Deliberately the same shape as gRenderer::gSceneLightData, since the OpenGL path
37 * already feeds that exact layout to its own shader.
38 */
39struct alignas(16) gVKLightData {
40 alignas(4) int type;
41 alignas(16) glm::vec3 position;
42 alignas(16) glm::vec3 direction;
43 alignas(16) glm::vec4 ambient;
44 alignas(16) glm::vec4 diffuse;
45 alignas(16) glm::vec4 specular;
46 alignas(4) float constant;
47 alignas(4) float linear;
48 alignas(4) float quadratic;
49 alignas(4) float cutoff;
50 alignas(4) float outercutoff;
51};
52
53/*
54 * The whole Scene uniform block, matching set 0 binding 0 of the mesh3d shaders.
55 * pad0/pad1 are not decoration: std140 starts the light array on a 16 byte
56 * boundary, so the two ints before it have to be followed by eight bytes of
57 * nothing, and naming them keeps that visible instead of accidental.
58 */
59struct alignas(16) gVKSceneUniforms {
60 alignas(16) glm::mat4 projection;
61 alignas(16) glm::mat4 view;
62 // World space -> the light's clip space, for looking a fragment up in the shadow
63 // map. Identity when there is no shadow map, in which case useshadowmap is 0 and
64 // the shader never uses it.
65 alignas(16) glm::mat4 lightmatrix;
66 alignas(16) glm::vec4 viewpos;
67 alignas(16) glm::vec4 globalambientcolor;
68 // The renderer's current draw colour, kept for layout only: both 3D shaders
69 // still declare this field, so the struct has to match, but neither reads it.
70 //
71 // It started here and had to move. color_frag.glsl ends with
72 // "result * renderColor", and OpenGL republishes the value inside setColor, so a
73 // canvas that recolours between two meshes gets a different colour on each. A
74 // uniform block cannot follow that: it is read when the commands execute, not
75 // when they are recorded, so every draw of the frame sees whichever value was
76 // written last. drawMesh3D folds the colour into the material push constant
77 // instead, which is per draw by construction.
78 alignas(16) glm::vec4 rendercolor;
79 // xyz is the shadow-casting light's position, which the depth bias is computed
80 // from; w is 0 or 1 for whether a shadow map is bound at all.
81 alignas(16) glm::vec4 shadowlightpos;
82 alignas(4) int lightnum;
83 alignas(4) int enabledlights;
84 // 5x5 PCF instead of 3x3, mirroring ENABLE_SOFT_SHADOWS on the OpenGL side.
85 alignas(4) int softshadows;
86 // The same bitfield color_frag.glsl reads, with the same bit values - see
87 // gRenderer::ENABLE_FOG and friends. Soft shadows keep their own field above,
88 // which predates this and is what the shadow code already reads; the bit is set
89 // here too so the two never disagree about what the renderer was asked for.
90 // SSAO's bit is carried but nothing acts on it: it needs a depth and normal
91 // prepass, which this backend does not have.
92 alignas(4) int flags;
93 // xyz the fog colour, w the mode (0 linear, 1 exponential). Packed into the
94 // spare component rather than given an int of its own, which std140 would have
95 // to pad out to sixteen bytes anyway.
96 alignas(16) glm::vec4 fogcolor;
97 // x density, y gradient, z linear start, w linear end.
98 alignas(16) glm::vec4 fogparams;
99 gVKLightData lights[GVK_MAX_LIGHTS];
100};
101
102/*
103 * The push constant block of the mesh3d pipeline. Held to 128 bytes, the smallest
104 * range Vulkan guarantees, so no implementation has to be asked for more.
105 */
106struct gVKMeshPush {
107 glm::mat4 model;
108 glm::vec4 ambient;
109 glm::vec4 diffuse;
110 glm::vec4 specular;
111 // x shininess, y map bitfield (diffuse/specular/normal), zw texture tiling.
112 glm::vec4 misc;
113};
114
115/*
116 * The push constant block of the PBR pipeline, matching pbr_vert/pbr_frag. Smaller than
117 * the one above because a PBR material carries no colours - everything comes from
118 * the maps, or from the shader's defaults where a map is absent.
119 */
120struct gVKPbrPush {
121 glm::mat4 model;
122 // maps0 = albedo, normal, metallic, roughness; maps1.x = ao. Non-zero means the
123 // material supplies that map.
124 glm::ivec4 maps0;
125 glm::ivec4 maps1;
126 // xy texture tiling; zw reserved.
127 glm::vec4 params;
128};
129
130/*
131 * The push constant block of the shadow pipeline, matching shadowmap_vert/_frag. lightmodel is
132 * lightProjection * lightView * model already multiplied out, so the depth pass needs
133 * no matrices of its own.
134 */
135struct gVKShadowPush {
136 glm::mat4 lightmodel;
137 // x is non-zero when the caster has a cutout diffuse map; the rest keeps the
138 // block vec4 aligned.
139 glm::vec4 misc;
140};
141
142// A frame can change its lights while recording (muzzle flashes are a common
143// example). Each change needs immutable storage until that frame's fence signals;
144// otherwise rewriting one mapped UBO retroactively changes earlier draws.
145/*
146 * Creates one uniform buffer and one descriptor set per frame in flight. Must run
147 * after the pipelines, because the set layout and the descriptor pool it allocates
148 * from are both produced while reflecting the mesh3d shaders.
149 */
150bool gvkCreateUniformResources(gVKContext& ctx);
151void gvkDestroyUniformResources(gVKContext& ctx);
152
153/*
154 * Adds one more chunk of scene uniform slots to a frame in flight. Not a file
155 * local helper because gVKContext has to befriend it: the storage it appends to
156 * is the context's own.
157 */
158bool gvkAppendSceneUniformChunk(gVKContext& ctx, int framei);
159
160/*
161 * Copies data into the current frame's buffer. Cheap: the buffer is host visible,
162 * host coherent and already mapped, so this is a memcpy with no flush and no
163 * synchronisation of its own - the frame's fence is what keeps it safe.
164 */
165bool gvkWriteSceneUniforms(gVKContext& ctx, const gVKSceneUniforms& data);
166
167#endif /* GVK_VULKAN */
168
169#endif /* CORE_GVKUNIFORM_H */