GlistEngine
Loading...
Searching...
No Matches
gVKUserShader.h
Go to the documentation of this file.
1/*
2 * gVKUserShader.h
3 *
4 * The Vulkan side of gShader: a shader an application wrote, loaded at run time
5 * and addressed by name.
6 *
7 * The two backends disagree about what a shader is. OpenGL compiles GLSL, links
8 * a program, and lets a caller reach any uniform in it by name at any time.
9 * Vulkan has no free-standing uniforms and no linking step: a shader is SPIR-V,
10 * its resources are descriptor sets and push constants, and the layout of both is
11 * fixed when the pipeline is created - together with the vertex layout, the blend
12 * state and the render pass. There is nothing to set a uniform on.
13 *
14 * What closes that gap is reflection. gVKReflect reads the compiled SPIR-V back
15 * and reports every uniform block member by name, offset and shape, so
16 * setVec3("lightpos", v) becomes a write at a known offset into a uniform buffer
17 * the module owns, and setInt("tex", 0) becomes a note that binding N samples
18 * whatever texture unit 0 holds. The application keeps the OpenGL-shaped API it
19 * already uses; the difference stays here.
20 *
21 * What an application has to change: nothing in its C++, but its GLSL has to be
22 * Vulkan GLSL. Loose uniforms are not a thing to translate into - they have to be
23 * gathered into a block with a set and binding, and samplers need a binding of
24 * their own. That is a shader source change, not an engine one, which is why the
25 * failure is reported by name rather than papered over.
26 *
27 * Where the SPIR-V comes from:
28 * - shaderc, when the build has it (desktop development builds). GLSL goes in
29 * as the application wrote it and comes out compiled, hot reload included.
30 * - glslang, when a desktop release has its runtime libraries. This compiles
31 * source-only shaders once when the application constructs the effect.
32 * - a .spv file beside the source otherwise. This is the mobile path and the
33 * fallback for a desktop system without either compiler: the shader is
34 * compiled ahead of time by glslc and shipped next to the GLSL - the same
35 * arrangement the engine's own shaders use through gVKShaders.h.
36 */
37
38#pragma once
39
40#ifndef CORE_GVKUSERSHADER_H
41#define CORE_GVKUSERSHADER_H
42
43#include "gVKContext.h"
44
45#ifdef GVK_VULKAN
46
47#include "gVKReflect.h"
48#include <string>
49#include <vector>
50
51struct gVKTexture;
52
53// Handles are opaque and never reused within a run. 0 means "no shader".
54using gVKUserShaderId = uint32_t;
55inline constexpr gVKUserShaderId GVK_NO_USER_SHADER = 0;
56
57// Shaders the engine builds through this path rather than as a pipeline of its
58// own, and which therefore have to work in a build that links no shader compiler.
59// There is one: gFbo's screen resolve, the pass that ends a post-process chain.
60// Its SPIR-V is compiled at build time from graphics/shaders/fbo_vert.glsl and
61// fbo_frag.glsl and lives in gVKShaders.h, and is used when compiling is not
62// possible.
63// Anything an application loads has no such fallback and passes NONE.
64enum gvkBuiltinShader {
65 GVK_BUILTIN_NONE = 0,
66 GVK_BUILTIN_FBO,
67};
68
69// Compiles and builds one shader from GLSL held in memory. sourcePath, when the
70// sources came from files, is where a pre-compiled .spv is looked for; leave it
71// empty for sources the application built in memory. Returns GVK_NO_USER_SHADER
72// on any failure, having logged why.
73gVKUserShaderId gvkCreateUserShader(gVKContext& ctx, const std::string& vertexSource,
74 const std::string& fragmentSource, const std::string& vertexPath,
75 const std::string& fragmentPath, int builtin = GVK_BUILTIN_NONE);
76
77// Releases one. The pipeline may still be recorded into a frame in flight, so the
78// caller is responsible for having waited; gVKRenderEngine does that at teardown.
79void gvkDestroyUserShader(gVKContext& ctx, gVKUserShaderId id);
80void gvkDestroyAllUserShaders(gVKContext& ctx);
81
82// Rebuilds every live shader's pipeline and descriptor sets. Called after a
83// swapchain or render pass rebuild, which invalidates both.
84bool gvkRebuildUserShaders(gVKContext& ctx);
85
86// gShader::use(). The bound shader is what the next draw records with; binding
87// GVK_NO_USER_SHADER puts the engine's own pipelines back.
88void gvkUseUserShader(gVKUserShaderId id);
89gVKUserShaderId gvkBoundUserShader();
90
91// Writes one value by name. component, rows and columns describe what data
92// points at - a glm::mat4 is FLOAT 4x4, a glm::vec3 is FLOAT 3x1 - and the write
93// is converted into whatever layout the block declared, which is not the same
94// thing: a mat3 is nine tight floats here and three sixteen-byte columns there.
95//
96// Returns false when the shader declares no such name, which is not an error in
97// itself: the OpenGL path silently ignores a uniform the shader optimised away,
98// and this keeps that behaviour rather than making every unused setter fatal.
99bool gvkSetUserShaderValue(gVKUserShaderId id, const std::string& name, const void* data,
100 gVKMemberComponent component, uint32_t rows, uint32_t columns);
101
102// Points a sampler at a texture unit, which is what setInt("tex", 0) means on the
103// OpenGL side.
104bool gvkSetUserShaderTexture(gVKUserShaderId id, const std::string& name, int unit);
105
106// True when the shader declares this name at all, block member or sampler.
107bool gvkUserShaderHasUniform(gVKUserShaderId id, const std::string& name);
108
109// How gVKUserShader reaches the texture bound to one unit without depending on
110// the renderer. gVKRenderEngine installs this at start-up.
111using gVKUserShaderTextureResolver = gVKTexture* (*)(int unit);
112void gvkSetUserShaderTextureResolver(gVKUserShaderTextureResolver resolver);
113
114// Records the bound shader - pipeline, descriptor sets and push constants - into
115// the current command buffer, ready for a draw. Returns false when no user shader
116// is bound or it could not be made ready, and the caller draws through the
117// engine's own pipeline instead.
118//
119// blending and lines pick the pipeline variant, the same way the mesh path picks
120// between the engine's own: blend state and topology class are baked into a
121// pipeline and cannot be changed by a command.
122bool gvkBindUserShaderForDraw(gVKContext& ctx, VkCommandBuffer cmd, bool blending, bool lines);
123
124// Bytes per vertex of the layout the bound shader's pipeline describes, and
125// whether that layout is the engine's mesh one. The fullscreen quad path checks
126// both before feeding a shader vertices it would misread.
127uint32_t gvkBoundUserShaderVertexStride();
128bool gvkBoundUserShaderUsesMeshLayout();
129
130#endif /* GVK_VULKAN */
131
132#endif /* CORE_GVKUSERSHADER_H */