Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
GPU.hpp
1#pragma once
2
3#include "resources/Mesh.hpp"
4#include "resources/Texture.hpp"
5#include "resources/Buffer.hpp"
6#include "graphics/GPUTypes.hpp"
7#include "graphics/Program.hpp"
8#include "Common.hpp"
9
10#include <functional>
11
12// Forward declarations.
13class Window;
14struct GPUContext;
15
20class GPU {
21
22 friend class GPUTexture;
23 friend class GPUBuffer;
24 friend class GPUMesh;
25 friend class Program;
26 friend class Swapchain;
27 friend class PipelineCache;
28
29public:
30
32 struct Metrics {
33 // Global statistics.
34 unsigned long long uploads = 0;
35 unsigned long long downloads = 0;
36 unsigned long long textures = 0;
37 unsigned long long buffers = 0;
38 unsigned long long programs = 0;
39 unsigned long long pipelines = 0;
40
41 // Per-frame statistics.
42 unsigned long long drawCalls = 0;
43 unsigned long long quadCalls = 0;
44 unsigned long long pipelineBindings = 0;
45 unsigned long long renderPasses = 0;
46 unsigned long long meshBindings = 0;
47 unsigned long long blitCount = 0;
48
51 drawCalls = 0;
52 quadCalls = 0;
54 renderPasses = 0;
55 meshBindings = 0;
56 blitCount = 0;
57 }
58 };
59
64 static bool setup(const std::string & appName);
65
70 static bool setupWindow(Window * window);
71
80 static void createGraphicsProgram(Program & program, const std::string & vertexContent, const std::string & fragmentContent, const std::string & tessControlContent, const std::string & tessEvalContent, const std::string & debugInfos);
81
87 static void createComputeProgram(Program & program, const std::string & computeContent, const std::string & debugInfos);
88
92 static void bindProgram(const Program & program);
93
104 static void beginRender(const Load & depthOp, const Load & stencilOp, const Texture * depthStencil, const Load & colorOp, const Texture * color0 = nullptr, const Texture * color1 = nullptr, const Texture * color2 = nullptr, const Texture * color3 = nullptr);
105
113 static void beginRender(const Load& colorOp, const Texture* color0, const Texture* color1 = nullptr, const Texture* color2 = nullptr, const Texture* color3 = nullptr);
114
120 static void beginRender(const Load& depthOp, const Load& stencilOp, const Texture* depthStencil);
121
134 static void beginRender(uint layer, uint mip, const Load & depthOp, const Load & stencilOp, const Texture * depthStencil, const Load & colorOp, const Texture * color0 = nullptr, const Texture * color1 = nullptr, const Texture * color2 = nullptr, const Texture * color3 = nullptr);
135
145 static void beginRender(uint layer, uint mip, const Load& colorOp, const Texture* color0, const Texture* color1 = nullptr, const Texture* color2 = nullptr, const Texture* color3 = nullptr);
146
154 static void beginRender(uint layer, uint mip, const Load& depthOp, const Load& stencilOp, const Texture* depthStencil);
155
162 static void beginRender(const Window & window, const Load & depthOp = Load::Operation::DONTCARE, const Load & stencilOp = Load::Operation::DONTCARE, const Load & colorOp = Load::Operation::DONTCARE);
163
164
166 static void endRender();
167
175 static void saveTexture(Texture & texture, const std::string & path, Image::Save options);
176
180 static void setupTexture(Texture & texture);
181
185 static void uploadTexture(const Texture & texture);
186
192 static void downloadTextureSync(Texture & texture);
193
200 static void downloadTextureSync(Texture & texture, int level);
201
210 static GPUAsyncTask downloadTextureAsync(const Texture& texture, const glm::uvec2& offset, const glm::uvec2& size, uint layerCount, std::function<void(const Texture&)> callback);
211
215 static void cancelAsyncOperation(const GPUAsyncTask& id);
216
220 static void generateMipMaps(const Texture & texture);
221
226 static void clearTexture(const Texture & texture, const glm::vec4& color);
227
232 static void clearDepth(const Texture & texture, float depth);
233
237 static void setupBuffer(Buffer & buffer);
238
245 static void uploadBuffer(const Buffer & buffer, size_t size, uchar * data, size_t offset = 0);
246
253 static void downloadBufferSync(const Buffer & buffer, size_t size, uchar * data, size_t offset = 0);
254
260 static void flushBuffer(const Buffer & buffer, size_t size, size_t offset);
261
266 static void setupMesh(Mesh & mesh);
267
271 static void drawMesh(const Mesh & mesh);
272
277 static void drawTesselatedMesh(const Mesh & mesh, uint patchSize);
278
296 static void drawQuad();
297
304 static void dispatch(uint width, uint height, uint depth);
305
308 static void flush();
309
312 static void nextFrame();
313
320 static void deviceInfos(std::string & vendor, std::string & renderer, std::string & version, std::string & shaderVersion);
321
325 static std::vector<std::string> supportedExtensions();
326
333 static void setViewport(int x, int y, int w, int h);
334
338 static void setViewport(const Texture& tex);
339
343 static void setDepthState(bool test);
344
350 static void setDepthState(bool test, TestFunction function, bool write);
351
356 static void setStencilState(bool test, bool write);
357
367 static void setStencilState(bool test, TestFunction function, StencilOp fail, StencilOp pass, StencilOp depthFail, uchar value);
368
372 static void setBlendState(bool test);
373
380 static void setBlendState(bool test, BlendEquation equation, BlendFunction src, BlendFunction dst);
381
385 static void setCullState(bool cull);
386
391 static void setCullState(bool cull, Faces culledFaces);
392
396 static void setPolygonState(PolygonMode mode);
397
404 static void setColorState(bool writeRed, bool writeGreen, bool writeBlue, bool writeAlpha);
405
409 static void getState(GPUState & state);
410
412 static const Metrics & getMetrics();
413
419 static void blitDepth(const Texture & src, const Texture & dst);
420
426 static void blit(const Texture & src, const Texture & dst, Filter filter);
427
435 static void blit(const Texture & src, const Texture & dst, size_t lSrc, size_t lDst, Filter filter);
436
446 static void blit(const Texture & src, const Texture & dst, size_t lSrc, size_t lDst, size_t mipSrc, size_t mipDst, Filter filter);
447
449 static GPUContext* getInternal();
450
452 static void cleanup();
453
454private:
455
468 static void bindAttachments(uint layer, uint mip, const Load& colorOp, const Load& depthOp, const Load& stencilOp, const Texture* depthStencil, const Texture* color0, const Texture* color1, const Texture* color2, const Texture* color3);
469
471 static void bindGraphicsPipelineIfNeeded();
472
474 static void bindComputePipelineIfNeeded();
475
477 static void endRenderingIfNeeded();
478
480 static void beginFrameCommandBuffers();
481
485 static void submitFrameCommandBuffers();
486
490 static void clean(GPUTexture & tex);
491
495 static void clean(GPUMesh & mesh);
496
500 static void clean(GPUBuffer & buffer);
501
505 static void clean(Program & program);
506
508 static void processDestructionRequests();
509
513 static void processAsyncTasks(bool forceAll = false);
514
519 static Mesh _quad;
520
521};
General purpose GPU buffer, with different use types determining its memory type, visibility and acce...
Definition: Buffer.hpp:10
Store data in a GPU buffer.
Definition: GPUObjects.hpp:86
Provide utility functions to communicate with the driver and GPU.
Definition: GPU.hpp:20
static void createGraphicsProgram(Program &program, const std::string &vertexContent, const std::string &fragmentContent, const std::string &tessControlContent, const std::string &tessEvalContent, const std::string &debugInfos)
Definition: GPU.cpp:330
static void setStencilState(bool test, bool write)
Definition: GPU.cpp:1558
static void setViewport(int x, int y, int w, int h)
Definition: GPU.cpp:1527
static void setPolygonState(PolygonMode mode)
Definition: GPU.cpp:1593
static Metrics _metricsPrevious
Internal metrics for the last completed frame.
Definition: GPU.hpp:518
static void downloadTextureSync(Texture &texture)
Definition: GPU.cpp:940
static void setupBuffer(Buffer &buffer)
Definition: GPU.cpp:1080
static void saveTexture(Texture &texture, const std::string &path, Image::Save options)
Definition: GPU.cpp:551
static void nextFrame()
Definition: GPU.cpp:1457
static void bindAttachments(uint layer, uint mip, const Load &colorOp, const Load &depthOp, const Load &stencilOp, const Texture *depthStencil, const Texture *color0, const Texture *color1, const Texture *color2, const Texture *color3)
Definition: GPU.cpp:423
static void deviceInfos(std::string &vendor, std::string &renderer, std::string &version, std::string &shaderVersion)
Definition: GPU.cpp:1469
static bool setupWindow(Window *window)
Definition: GPU.cpp:178
static void bindGraphicsPipelineIfNeeded()
Definition: GPU.cpp:1305
static Mesh _quad
Screen-covering triangle.
Definition: GPU.hpp:519
static void generateMipMaps(const Texture &texture)
Definition: GPU.cpp:944
static void clean(GPUTexture &tex)
Definition: GPU.cpp:1721
static void createComputeProgram(Program &program, const std::string &computeContent, const std::string &debugInfos)
Definition: GPU.cpp:370
static void flush()
Definition: GPU.cpp:1443
static void beginRender(const Load &depthOp, const Load &stencilOp, const Texture *depthStencil, const Load &colorOp, const Texture *color0=nullptr, const Texture *color1=nullptr, const Texture *color2=nullptr, const Texture *color3=nullptr)
Definition: GPU.cpp:394
static void dispatch(uint width, uint height, uint depth)
Definition: GPU.cpp:1389
static void beginFrameCommandBuffers()
Definition: GPU.cpp:1413
static GPUAsyncTask downloadTextureAsync(const Texture &texture, const glm::uvec2 &offset, const glm::uvec2 &size, uint layerCount, std::function< void(const Texture &)> callback)
Definition: GPU.cpp:906
static void getState(GPUState &state)
Definition: GPU.cpp:1683
static void submitFrameCommandBuffers()
Definition: GPU.cpp:1423
static void drawMesh(const Mesh &mesh)
Definition: GPU.cpp:1356
static Metrics _metrics
Internal metrics (draw count, state changes,...).
Definition: GPU.hpp:517
static const Metrics & getMetrics()
Definition: GPU.cpp:1688
static void cancelAsyncOperation(const GPUAsyncTask &id)
Definition: GPU.cpp:931
static void setupMesh(Mesh &mesh)
Definition: GPU.cpp:1218
static void endRender()
Definition: GPU.cpp:1657
static bool setup(const std::string &appName)
Definition: GPU.cpp:44
static GPUContext * getInternal()
Definition: GPU.cpp:40
static void setCullState(bool cull)
Definition: GPU.cpp:1584
static void endRenderingIfNeeded()
Definition: GPU.cpp:1647
static void clearDepth(const Texture &texture, float depth)
Definition: GPU.cpp:1054
static void processDestructionRequests()
Definition: GPU.cpp:1786
static void clearTexture(const Texture &texture, const glm::vec4 &color)
Definition: GPU.cpp:1027
static std::vector< std::string > supportedExtensions()
Definition: GPU.cpp:1493
static void uploadTexture(const Texture &texture)
Definition: GPU.cpp:718
static void setupTexture(Texture &texture)
Definition: GPU.cpp:575
static void cleanup()
Definition: GPU.cpp:1692
static void setColorState(bool writeRed, bool writeGreen, bool writeBlue, bool writeAlpha)
Definition: GPU.cpp:1597
static void setDepthState(bool test)
Definition: GPU.cpp:1548
static void downloadBufferSync(const Buffer &buffer, size_t size, uchar *data, size_t offset=0)
Definition: GPU.cpp:1173
static GPUState _state
Current GPU state.
Definition: GPU.hpp:515
static void bindComputePipelineIfNeeded()
Definition: GPU.cpp:1331
static void blitDepth(const Texture &src, const Texture &dst)
Definition: GPU.cpp:1604
static void bindProgram(const Program &program)
Definition: GPU.cpp:386
static void processAsyncTasks(bool forceAll=false)
Definition: GPU.cpp:1815
static void drawTesselatedMesh(const Mesh &mesh, uint patchSize)
Definition: GPU.cpp:1370
static void uploadBuffer(const Buffer &buffer, size_t size, uchar *data, size_t offset=0)
Definition: GPU.cpp:1135
static GPUState _lastState
Previous draw call GPU state for caching.
Definition: GPU.hpp:516
static void flushBuffer(const Buffer &buffer, size_t size, size_t offset)
Definition: GPU.cpp:1210
static void blit(const Texture &src, const Texture &dst, Filter filter)
Definition: GPU.cpp:1608
static void setBlendState(bool test)
Definition: GPU.cpp:1573
static void drawQuad()
Definition: GPU.cpp:1376
Store vertices and triangles data on the GPU, linked by an array object.
Definition: GPUObjects.hpp:125
Internal GPU state ; not all API options are exposed, only these that can be toggled in Rendu.
Definition: GPUTypes.hpp:324
Store a texture data on the GPU.
Definition: GPUObjects.hpp:18
Save
Options for saving an image to disk.
Definition: Image.hpp:13
Represents a geometric mesh composed of vertices, other attributes and triangles.
Definition: Mesh.hpp:15
Create and reuse GPU pipelines based on a given state. This supports both graphics and compute pipeli...
Definition: PipelineCache.hpp:18
Represents a group of shaders used for rendering.
Definition: Program.hpp:31
A swapchain handles the creation and presentation of the backbuffer, along with GPU work submission a...
Definition: Swapchain.hpp:16
Represents a texture containing one or more images, stored on the CPU and/or GPU.
Definition: Texture.hpp:12
Represent an OS window and its associated rendering context.
Definition: Window.hpp:14
BlendFunction
How the source and destination values to blend are obtained from the pixel data by scaling.
Definition: GPUTypes.hpp:110
BlendEquation
Blending mix equation for each component. Below we use src and dst to denote the (modulated by the bl...
Definition: GPUTypes.hpp:95
Filter
The filtering mode of a texture: we deduce the magnification filter from the minification filter for ...
Definition: GPUTypes.hpp:198
StencilOp
Stencil operation to perform.
Definition: GPUTypes.hpp:76
PolygonMode
How polygons should be rasterized.
Definition: GPUTypes.hpp:141
TestFunction
Depth or stencil test function.
Definition: GPUTypes.hpp:59
Faces
Used to select a subset of faces. Front faces are defined counter-clockwise.
Definition: GPUTypes.hpp:129
Definition: GPU.hpp:32
unsigned long long downloads
Data download from the GPU.
Definition: GPU.hpp:35
unsigned long long buffers
Buffers created.
Definition: GPU.hpp:37
void resetPerFrameMetrics()
Reset metrics that are measured over one frame.
Definition: GPU.hpp:50
unsigned long long textures
Textures created.
Definition: GPU.hpp:36
unsigned long long blitCount
Texture blitting operations.
Definition: GPU.hpp:47
unsigned long long programs
Programs created.
Definition: GPU.hpp:38
unsigned long long quadCalls
Full screen quad.
Definition: GPU.hpp:43
unsigned long long meshBindings
Number of mesh bindings.
Definition: GPU.hpp:46
unsigned long long renderPasses
Number of render passes.
Definition: GPU.hpp:45
unsigned long long pipelineBindings
Number of pipeline set operations.
Definition: GPU.hpp:44
unsigned long long pipelines
Pipelines created.
Definition: GPU.hpp:39
unsigned long long uploads
Data upload to the GPU.
Definition: GPU.hpp:34
unsigned long long drawCalls
Mesh draw call.
Definition: GPU.hpp:42
Global GPU context, storing all structures used for resource allocation and tracking,...
Definition: GPUInternal.hpp:72
Detailed operation to perform when binding a texture (starting a renderpass).
Definition: GPUTypes.hpp:278
@ DONTCARE
Anything can be done, usually because we will overwrite data everywhere.