Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
Program.hpp
1#pragma once
2
3#include "Common.hpp"
4
5#include "graphics/GPUTypes.hpp"
6#include "resources/Buffer.hpp"
7#include "resources/Texture.hpp"
8
9#include <unordered_map>
10#include <array>
11
12// Forward declarations
13VK_DEFINE_HANDLE(VkBuffer)
14VK_DEFINE_HANDLE(VkImageView)
15VK_DEFINE_HANDLE(VkSampler)
16VK_DEFINE_HANDLE(VkShaderModule)
17VK_DEFINE_HANDLE(VkPipelineLayout)
18VK_DEFINE_HANDLE(VkDescriptorSetLayout)
19
20#define UNIFORMS_SET 0
21#define SAMPLERS_SET 1
22#define IMAGES_SET 2
23#define BUFFERS_SET 3
24
31class Program {
32public:
33
35 static uint ALL_MIPS;
36
38 enum class Type {
39 GRAPHICS,
40 COMPUTE
41 };
42
45 struct UniformDef {
46
48 enum class Type {
49 BOOL, BVEC2, BVEC3, BVEC4,
50 INT, IVEC2, IVEC3, IVEC4,
51 UINT, UVEC2, UVEC3, UVEC4,
52 FLOAT, VEC2, VEC3, VEC4,
53 MAT2, MAT3, MAT4,
54 OTHER
55 };
56
57 std::string name;
59
61 struct Location {
62 uint binding;
63 uint offset;
64 };
65
66 std::vector<Location> locations;
67
68 };
69
72 struct ImageDef {
73 std::string name;
75 uint binding;
76 uint set;
77 uint count;
78 bool storage;
79 };
80
83 struct BufferDef {
84 std::vector<UniformDef> members;
85 std::string name;
86 uint binding;
87 uint size;
88 uint set;
89 uint count;
90 bool storage;
91 };
92
93 using Uniforms = std::unordered_map<std::string, UniformDef>;
94
103 Program(const std::string & name, const std::string & vertexContent, const std::string & fragmentContent, const std::string & tessControlContent = "", const std::string & tessEvalContent = "");
104
110 Program(const std::string & name, const std::string & computeContent);
111
119 void reload(const std::string & vertexContent, const std::string & fragmentContent, const std::string & tessControlContent = "", const std::string & tessEvalContent = "");
120
125 void reload(const std::string & computeContent);
126
128 bool reloaded() const;
129
134 bool reloaded(bool absorb);
135
138 void use() const;
139
142 void clean();
143
148 void buffer(const UniformBufferBase& buffer, uint slot);
149
154 void buffer(const Buffer& buffer, uint slot);
155
160 void bufferArray(const std::vector<const Buffer *> & buffers, uint slot);
161
167 void texture(const Texture* texture, uint slot, uint mip = Program::ALL_MIPS);
168
174 void texture(const Texture& texture, uint slot, uint mip = Program::ALL_MIPS);
175
181 void textureArray(const std::vector<const Texture *> & textures, uint slot, uint mip = Program::ALL_MIPS);
182
188 void textures(const std::vector<const Texture *> & textures, size_t slot = 0);
189
193 void defaultTexture(uint slot);
194
200
202 void update();
203
208 void uniform(const std::string & name, bool t);
209
214 void uniform(const std::string & name, int t);
215
220 void uniform(const std::string & name, uint t);
221
226 void uniform(const std::string & name, float t);
227
232 void uniform(const std::string & name, const glm::vec2 & t);
233
238 void uniform(const std::string & name, const glm::vec3 & t);
239
244 void uniform(const std::string & name, const glm::vec4 & t);
245
250 void uniform(const std::string & name, const glm::ivec2 & t);
251
256 void uniform(const std::string & name, const glm::ivec3 & t);
257
262 void uniform(const std::string & name, const glm::ivec4 & t);
263
268 void uniform(const std::string & name, const glm::mat3 & t);
269
274 void uniform(const std::string & name, const glm::mat4 & t);
275
280 void getUniform(const std::string & name, bool & t) const;
281
286 void getUniform(const std::string & name, int & t) const;
287
292 void getUniform(const std::string & name, uint & t) const;
293
298 void getUniform(const std::string & name, float & t) const;
299
304 void getUniform(const std::string & name, glm::vec2 & t) const;
305
310 void getUniform(const std::string & name, glm::vec3 & t) const;
311
316 void getUniform(const std::string & name, glm::vec4 & t) const;
317
322 void getUniform(const std::string & name, glm::ivec2 & t) const;
323
328 void getUniform(const std::string & name, glm::ivec3 & t) const;
329
334 void getUniform(const std::string & name, glm::ivec4 & t) const;
335
340 void getUniform(const std::string & name, glm::mat3 & t) const;
341
346 void getUniform(const std::string & name, glm::mat4 & t) const;
347
350 const Uniforms & uniforms() const {
351 return _uniforms;
352 }
353
355 const std::string & name() const {
356 return _name;
357 }
358
361 return _type;
362 }
363
365 const glm::uvec3 & size() const;
366
370 Program & operator=(const Program &) = delete;
371
373 Program(const Program &) = delete;
374
378 Program & operator=(Program &&) = delete;
379
381 Program(Program &&) = default;
382
384 struct State {
385 std::vector<VkDescriptorSetLayout> setLayouts;
386 VkPipelineLayout layout = VK_NULL_HANDLE;
387 };
388
390 const State& getState() const {
391 return _state;
392 }
393
395 struct Stage {
396 std::vector<ImageDef> images;
397 std::vector<BufferDef> buffers;
398 VkShaderModule module = VK_NULL_HANDLE;
399 glm::uvec3 size = glm::uvec3(0);
401 void reset();
402 };
403
409 return _stages[uint(type)];
410 }
411
412private:
413
415 void reflect();
416
417
419 void updateUniformMetric() const;
420
425 inline const char* retrieveUniform(const UniformDef::Location& location) const {
426 return &(_dynamicBuffers.at(location.binding).buffer->data[location.offset]);
427 }
428
434 inline char* retrieveUniformNonConst(const UniformDef::Location& location) {
435 DynamicBufferState& buffState = _dynamicBuffers.at(location.binding);
436 buffState.dirty = true;
437 _dirtySets[0] = true;
438 return &(buffState.buffer->data[location.offset]);
439 }
440
443 std::shared_ptr<UniformBuffer<char>> buffer;
445 bool dirty = true;
446 };
447
450 std::string name;
451 TextureShape shape = TextureShape::D2;
452 std::vector<const Texture*> textures = { nullptr };
453 std::vector<VkImageView> views = { VK_NULL_HANDLE };
454 uint count = 1;
455 uint mip = 0xFFFF;
456 bool storage = false;
457 };
458
461 std::string name;
462 std::vector<VkBuffer> buffers = { VK_NULL_HANDLE};
463 std::vector<uint> offsets = {0};
464 uint size = 0;
465 uint count = 1;
466 uint lastSet = 0;
467 bool storage = false;
468 };
469
470
471 std::string _name;
472 std::array<Stage, int(ShaderType::COUNT)> _stages;
474
475 std::unordered_map<std::string, UniformDef> _uniforms;
476
477 std::unordered_map<int, DynamicBufferState> _dynamicBuffers;
478 std::unordered_map<int, TextureState> _textures;
479 std::unordered_map<int, StaticBufferState> _staticBuffers;
480
481 std::array<bool, 4> _dirtySets;
482 std::array<DescriptorSet, 4> _currentSets;
483 std::vector<uint32_t> _currentOffsets;
484
485 bool _reloaded = false;
486 const Type _type;
487
488 friend class GPU;
489};
General purpose GPU buffer, with different use types determining its memory type, visibility and acce...
Definition: Buffer.hpp:10
Provide utility functions to communicate with the driver and GPU.
Definition: GPU.hpp:20
Represents a group of shaders used for rendering.
Definition: Program.hpp:31
void textureArray(const std::vector< const Texture * > &textures, uint slot, uint mip=Program::ALL_MIPS)
Definition: Program.cpp:642
std::vector< uint32_t > _currentOffsets
Offsets in the descriptor set for dynamic uniform buffers.
Definition: Program.hpp:483
static uint ALL_MIPS
Definition: Program.hpp:35
void update()
Definition: Program.cpp:387
std::unordered_map< std::string, UniformDef > Uniforms
List of named uniforms.
Definition: Program.hpp:93
Stage & stage(ShaderType type)
Definition: Program.hpp:408
void transitionResourcesTo(Program::Type type)
Definition: Program.cpp:327
void reload(const std::string &vertexContent, const std::string &fragmentContent, const std::string &tessControlContent="", const std::string &tessEvalContent="")
Definition: Program.cpp:21
char * retrieveUniformNonConst(const UniformDef::Location &location)
Definition: Program.hpp:434
const Type _type
Is this a compute shader.
Definition: Program.hpp:486
const std::string & name() const
Definition: Program.hpp:355
std::array< Stage, int(ShaderType::COUNT)> _stages
Per-stage reflection data.
Definition: Program.hpp:472
Program & operator=(Program &&)=delete
State _state
Program pipeline state.
Definition: Program.hpp:473
std::array< bool, 4 > _dirtySets
Marks which descriptor sets are dirty.
Definition: Program.hpp:481
const State & getState() const
Definition: Program.hpp:390
void uniform(const std::string &name, bool t)
Definition: Program.cpp:690
std::unordered_map< int, DynamicBufferState > _dynamicBuffers
Dynamic uniform buffer definitions (set 0).
Definition: Program.hpp:477
std::unordered_map< int, TextureState > _textures
Dynamic image-sampler definitions (set 2).
Definition: Program.hpp:478
void reflect()
Reflect all uniforms/textures/storage buffers and images based on the shader content.
Definition: Program.cpp:49
std::string _name
Debug name.
Definition: Program.hpp:471
Program(const Program &)=delete
Program(Program &&)=default
Program & operator=(const Program &)=delete
const char * retrieveUniform(const UniformDef::Location &location) const
Definition: Program.hpp:425
const glm::uvec3 & size() const
Definition: Program.cpp:921
void clean()
Definition: Program.cpp:552
void use() const
Definition: Program.cpp:548
std::unordered_map< std::string, UniformDef > _uniforms
All dynamic uniform definitions.
Definition: Program.hpp:475
std::array< DescriptorSet, 4 > _currentSets
Descriptor sets.
Definition: Program.hpp:482
void textures(const std::vector< const Texture * > &textures, size_t slot=0)
Definition: Program.cpp:682
void texture(const Texture *texture, uint slot, uint mip=Program::ALL_MIPS)
Definition: Program.cpp:664
bool _reloaded
Has the program been reloaded.
Definition: Program.hpp:485
void defaultTexture(uint slot)
Definition: Program.cpp:668
bool reloaded() const
Definition: Program.cpp:536
Program::Type type() const
Definition: Program.hpp:360
std::unordered_map< int, StaticBufferState > _staticBuffers
Static uniform buffer definitions (set 3).
Definition: Program.hpp:479
const Uniforms & uniforms() const
Definition: Program.hpp:350
void getUniform(const std::string &name, bool &t) const
Definition: Program.cpp:824
Type
Definition: Program.hpp:38
@ GRAPHICS
Graphics program for draw calls.
@ COMPUTE
Compute program for dispatch calls.
void updateUniformMetric() const
Update internal metrics.
Definition: Program.cpp:931
void bufferArray(const std::vector< const Buffer * > &buffers, uint slot)
Definition: Program.cpp:604
void buffer(const UniformBufferBase &buffer, uint slot)
Definition: Program.cpp:576
Represents a texture containing one or more images, stored on the CPU and/or GPU.
Definition: Texture.hpp:12
Uniform buffer exposed to all shader stages, that can be updated at varying frequencies....
Definition: Buffer.hpp:105
TextureShape
The shape of a texture: dimensions, layers organisation.
Definition: GPUTypes.hpp:153
ShaderType
The type of a shader.
Definition: GPUTypes.hpp:18
Buffer reflection information.
Definition: Program.hpp:83
bool storage
Is this a storage buffer.
Definition: Program.hpp:90
uint count
Number of similar buffers for this binding point.
Definition: Program.hpp:89
std::string name
Buffer name.
Definition: Program.hpp:85
std::vector< UniformDef > members
Uniforms in buffer.
Definition: Program.hpp:84
uint set
Buffer binding set.
Definition: Program.hpp:88
uint size
Buffer size.
Definition: Program.hpp:87
uint binding
Buffer binding location.
Definition: Program.hpp:86
Internal state for a dynamic uniform buffer.
Definition: Program.hpp:442
std::shared_ptr< UniformBuffer< char > > buffer
Owned Uniform buffer.
Definition: Program.hpp:443
uint descriptorIndex
Descriptor index in set.
Definition: Program.hpp:444
bool dirty
Is the buffer dirty since last draw.
Definition: Program.hpp:445
Image-sampler reflection information.
Definition: Program.hpp:72
uint set
Image binding set.
Definition: Program.hpp:76
std::string name
Image name.
Definition: Program.hpp:73
uint count
Number of similar images for this binding point.
Definition: Program.hpp:77
uint binding
Image binding location.
Definition: Program.hpp:75
bool storage
Is this a storage image.
Definition: Program.hpp:78
TextureShape shape
Image shape.
Definition: Program.hpp:74
Per-stage reflection information.
Definition: Program.hpp:395
std::vector< ImageDef > images
Image definitions.
Definition: Program.hpp:396
std::vector< BufferDef > buffers
Buffers definitions.
Definition: Program.hpp:397
glm::uvec3 size
Definition: Program.hpp:399
VkShaderModule module
Native shader data.
Definition: Program.hpp:398
void reset()
Reset the stage state.
Definition: Program.cpp:925
Program pipeline state.
Definition: Program.hpp:384
VkPipelineLayout layout
Layout handle (pre-created).
Definition: Program.hpp:386
std::vector< VkDescriptorSetLayout > setLayouts
Descriptor sets layouts.
Definition: Program.hpp:385
Internal state for a static (external) uniform buffer.
Definition: Program.hpp:460
std::vector< uint > offsets
Start offset in the buffer.
Definition: Program.hpp:463
uint size
Region size in the buffer.
Definition: Program.hpp:464
uint count
Number of buffers bound at this slot.
Definition: Program.hpp:465
std::string name
Name.
Definition: Program.hpp:461
bool storage
Is the buffer used as storage.
Definition: Program.hpp:467
std::vector< VkBuffer > buffers
Native buffer handle.
Definition: Program.hpp:462
uint lastSet
Keep track of one of the buffers that has been set (and is thus valid).
Definition: Program.hpp:466
Internal state for an image.
Definition: Program.hpp:449
bool storage
Is the image used as storage.
Definition: Program.hpp:456
std::string name
Name.
Definition: Program.hpp:450
std::vector< VkImageView > views
Texture view.
Definition: Program.hpp:453
std::vector< const Texture * > textures
The source texture.
Definition: Program.hpp:452
uint count
Number of images bound at this slot..
Definition: Program.hpp:454
TextureShape shape
Texture shape.
Definition: Program.hpp:451
uint mip
The corresponding mip.
Definition: Program.hpp:455
Uniform location.
Definition: Program.hpp:61
uint offset
Offset in buffer.
Definition: Program.hpp:63
uint binding
Buffer binding.
Definition: Program.hpp:62
Uniform reflection information.
Definition: Program.hpp:45
std::vector< Location > locations
Locations where this uniform is present.
Definition: Program.hpp:66
Type type
The uniform type.
Definition: Program.hpp:58
std::string name
The uniform name.
Definition: Program.hpp:57
Type
Uniform basic type.
Definition: Program.hpp:48