JustAnotherDnDGame 0.1.0
Jeu de rôle tactique au d20, vue de dessus, en C++/Qt
Loading...
Searching...
No Matches
SpriteBatch.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Valentin Eloy
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#pragma once
5
6#include <cstddef>
7#include <memory>
8#include <unordered_map>
9#include <vector>
10
11#include <DirectXMath.h>
12
13#include "HMI/Graphics/Quad.h"
15
16class QRhi;
17class QRhiBuffer;
18class QRhiCommandBuffer;
19class QRhiGraphicsPipeline;
20class QRhiRenderPassDescriptor;
21class QRhiRenderTarget;
22class QRhiResourceUpdateBatch;
23class QRhiSampler;
24class QRhiShaderResourceBindings;
25class QRhiTexture;
26
37
38namespace hmi {
39
60public:
65 explicit SpriteBatch(QRhi* rhi);
66
68
69 SpriteBatch(const SpriteBatch&) = delete;
71
73 [[nodiscard]] QRhi* rhi() const noexcept {
74 return _rhi;
75 }
76
78 void beginFrame();
79
86 void begin(const DirectX::XMFLOAT4X4& projection, TextureHandle texture);
87
92 void draw(const SpriteQuad& quad);
93
99 void draw(const LineQuad& line);
100
102 void end();
103
116 void submit(QRhiCommandBuffer* commandBuffer, QRhiRenderTarget* target,
117 QRhiResourceUpdateBatch* updates, const float clear[4]);
118
119private:
121 struct Vertex {
122 float x;
123 float y;
124 float u;
125 float v;
126 float r;
127 float g;
128 float b;
129 float a;
130 };
131
133 struct Batch {
134 QRhiTexture* texture = nullptr;
138 std::size_t firstQuad = 0;
140 std::size_t quadCount = 0;
141 };
142
144 static constexpr std::size_t MAXIMUM_QUADS = 65536 / 4;
145
147 bool ensurePipeline(QRhiRenderTarget* target);
149 bool ensureVertexCapacity(std::size_t quadCount);
151 bool ensureUniformCapacity(std::size_t batchCount);
153 QRhiShaderResourceBindings* bindingsFor(QRhiTexture* texture);
155 void closeBatch();
156
157 QRhi* _rhi; // non possédé
158 std::unique_ptr<QRhiBuffer> _vertexBuffer;
159 std::unique_ptr<QRhiBuffer> _indexBuffer;
160 std::unique_ptr<QRhiBuffer> _uniformBuffer;
161 std::unique_ptr<QRhiSampler> _sampler;
162 std::unique_ptr<QRhiGraphicsPipeline> _pipeline;
165 QRhiRenderPassDescriptor* _pipelinePass = nullptr;
168 std::unordered_map<QRhiTexture*, std::unique_ptr<QRhiShaderResourceBindings>> _bindings;
171 std::unique_ptr<QRhiShaderResourceBindings> _layoutBindings;
174 QRhiResourceUpdateBatch* _pendingIndexUpload = nullptr;
176 std::size_t _uniformSlots = 0;
179
180 std::vector<Vertex> _vertices;
181 std::vector<Batch> _batches;
184 bool _recording = false;
186 std::vector<DirectX::XMFLOAT4X4> _projections;
187};
188
189} // namespace hmi
Primitives de dessin 2D (quads texturés), indépendantes de Direct3D.
Ordonnancement de calques unique et explicite du rendu (EX-REN-014).
SpriteBatch(const SpriteBatch &)=delete
std::unique_ptr< QRhiSampler > _sampler
Definition SpriteBatch.h:161
SpriteBatch(QRhi *rhi)
Construit le pipeline (shaders, tampons, échantillonneur) sur une interface QRhi.
Definition SpriteBatch.cpp:60
int _uniformStride
Taille alignée d'un bloc de projection, imposée par le matériel.
Definition SpriteBatch.h:178
bool _recording
Definition SpriteBatch.h:184
bool ensureUniformCapacity(std::size_t batchCount)
Redimensionne le tampon uniforme pour batchCount emplacements de projection.
Definition SpriteBatch.cpp:114
bool ensurePipeline(QRhiRenderTarget *target)
Crée (ou recrée) le pipeline pour la passe de rendu donnée. Idempotent.
Definition SpriteBatch.cpp:171
std::unique_ptr< QRhiShaderResourceBindings > _layoutBindings
Liaisons de référence, construites une fois : QRhi exige que toutes les liaisons employées par un pip...
Definition SpriteBatch.h:171
std::unique_ptr< QRhiBuffer > _vertexBuffer
Definition SpriteBatch.h:158
std::vector< Batch > _batches
Definition SpriteBatch.h:181
static constexpr std::size_t MAXIMUM_QUADS
Nombre de quads couverts par le tampon d'indices, et donc par un seul appel de dessin.
Definition SpriteBatch.h:144
SpriteBatch & operator=(const SpriteBatch &)=delete
std::size_t _uniformSlots
Capacité du tampon uniforme, en nombre d'emplacements de projection.
Definition SpriteBatch.h:176
void begin(const DirectX::XMFLOAT4X4 &projection, TextureHandle texture)
Démarre un lot : fixe la texture échantillonnée et la projection.
Definition SpriteBatch.cpp:245
QRhi * rhi() const noexcept
Definition SpriteBatch.h:73
void submit(QRhiCommandBuffer *commandBuffer, QRhiRenderTarget *target, QRhiResourceUpdateBatch *updates, const float clear[4])
Téléverse l'image enregistrée et l'émet en une passe de rendu.
Definition SpriteBatch.cpp:322
QRhiShaderResourceBindings * bindingsFor(QRhiTexture *texture)
Definition SpriteBatch.cpp:149
void closeBatch()
Ferme le lot en cours d'enregistrement, s'il y en a un.
Definition SpriteBatch.cpp:256
std::unique_ptr< QRhiBuffer > _uniformBuffer
Definition SpriteBatch.h:160
std::vector< Vertex > _vertices
Definition SpriteBatch.h:180
std::unique_ptr< QRhiGraphicsPipeline > _pipeline
Definition SpriteBatch.h:162
void beginFrame()
Ouvre l'enregistrement d'une image : vide les sommets et les lots de la précédente.
Definition SpriteBatch.cpp:237
void end()
Termine le lot : fige la plage de quads enregistrée. Un lot vide n'émettra aucun dessin.
Definition SpriteBatch.cpp:317
QRhiResourceUpdateBatch * _pendingIndexUpload
Téléversement du tampon d'indices, en attente de la première image : un tampon immuable se remplit un...
Definition SpriteBatch.h:174
std::unique_ptr< QRhiBuffer > _indexBuffer
Definition SpriteBatch.h:159
std::unordered_map< QRhiTexture *, std::unique_ptr< QRhiShaderResourceBindings > > _bindings
Liaisons par texture, gardées d'une image à l'autre : les recréer à chaque lot allouerait des ressour...
Definition SpriteBatch.h:168
std::vector< DirectX::XMFLOAT4X4 > _projections
Projections des lots, dans l'ordre d'enregistrement (téléversées d'un bloc au submit).
Definition SpriteBatch.h:186
void draw(const SpriteQuad &quad)
Ajoute un quad au lot courant.
Definition SpriteBatch.cpp:268
bool ensureVertexCapacity(std::size_t quadCount)
Redimensionne le tampon de sommets si l'image enregistrée n'y tient pas.
Definition SpriteBatch.cpp:134
QRhiRenderPassDescriptor * _pipelinePass
Descripteur de la passe pour laquelle _pipeline a été construit : un changement de cible (redimension...
Definition SpriteBatch.h:165
Batch _current
Lot en cours d'enregistrement (entre begin() et end()), s'il y en a un.
Definition SpriteBatch.h:183
QRhi * _rhi
Definition SpriteBatch.h:157
Definition AudioEngine.cpp:15
void * TextureHandle
Identité opaque d'une texture liée, du point de vue de la composition.
Definition RenderLayer.h:107
Un segment épais à dessiner : deux extrémités en unités monde, une épaisseur (perpendiculaire au segm...
Definition Quad.h:53
Un lot enregistré : une texture, une projection, une plage contiguë de quads.
Definition SpriteBatch.h:133
std::size_t firstQuad
Rang du premier quad du lot dans le tampon de sommets.
Definition SpriteBatch.h:138
std::size_t quadCount
Nombre de quads du lot.
Definition SpriteBatch.h:140
int uniformOffset
Décalage du bloc de projection de ce lot dans le tampon uniforme, en octets.
Definition SpriteBatch.h:136
QRhiTexture * texture
Definition SpriteBatch.h:134
Sommet envoyé au GPU : position monde (x, y), UV, couleur RVBA.
Definition SpriteBatch.h:121
float g
Definition SpriteBatch.h:127
float y
Definition SpriteBatch.h:123
float a
Definition SpriteBatch.h:129
float x
Definition SpriteBatch.h:122
float v
Definition SpriteBatch.h:125
float b
Definition SpriteBatch.h:128
float r
Definition SpriteBatch.h:126
float u
Definition SpriteBatch.h:124
Un quad texturé à dessiner : rectangle en unités monde, région de texture (coordonnées normalisées) e...
Definition Quad.h:29