25#include <initializer_list>
27#include <unordered_map>
28#include <unordered_set>
82 const u32* instanceCounts,
u32 drawCount)
override;
122 const void* data,
u32 byteLength,
u32 mipLevels)
override;
127 const void* pixels,
bool flipY)
override;
167 static u32 nameOf(
const std::vector<u32>& names,
u32 id) {
168 return id < names.size() ? names[id] : 0u;
170 static void setName(std::vector<u32>& names,
u32 id,
u32 name) {
171 if (
id >= names.size()) names.resize(
static_cast<usize>(
id) + 1, 0u);
175 bool isRenderbuffer(
u32 textureId)
const;
181 bool boundFramebufferComplete(std::initializer_list<TextureHandle> attachments);
183 std::unordered_map<std::string, bool> framebuffer_complete_;
188 void setClearStencil(
i32 value);
189 void clear(
bool color,
bool depth,
bool stencil);
193 void setBlendEnabled(
bool enabled);
194 void setBlendMode(
BlendMode mode,
bool layerAlpha =
false);
195 void setDepthTest(
bool enabled);
196 void setDepthWrite(
bool enabled);
197 void setStencilTest(
bool enabled);
200 void setStencilMask(
u32 mask);
201 void setColorMask(
bool r,
bool g,
bool b,
bool a);
202 void setCulling(
bool enabled);
203 void setCullFace(
bool front);
204 void setDepthBias(
i16 bias);
208 void uploadBufferStore(
u32 id,
u32 offsetBytes,
const void* data,
u32 sizeBytes,
bool respec);
213 void resetStateCache();
219 void prepareVertexState();
224 void bindTextureForEdit(
u32 name);
229 void evictSamplerBinding(
u32 name);
232 u32 textureTarget(
u32 name)
const;
234 std::unordered_set<u32> array_names_;
236 std::unordered_set<u32> volume_names_;
239 std::vector<u32> buffer_names_;
240 std::vector<u32> texture_names_;
241 std::vector<u32> program_names_;
243 struct PendingProgram {
u32 vertex = 0;
u32 fragment = 0; std::string label; };
244 std::unordered_map<u32, PendingProgram> pending_programs_;
248 bool pass_discard_msaa_ =
false;
249 bool pass_discard_depth_ =
false;
251 i8 default_depth_bits_ = -1;
252 i8 default_stencil_bits_ = -1;
254 void discardLeftDepth();
256 std::unordered_set<u32> failed_programs_;
258 i8 completion_status_ = 0;
259 std::vector<u32> framebuffer_names_;
260 std::vector<u32> query_names_;
262 std::vector<u32> uniform_slots_;
264 struct UniformRange {
u32 offset = 0;
u32 size = 0; };
265 std::vector<UniformRange> uniform_ranges_;
266 void bindUniformSlot(
u32 slot,
u32 name);
271 enum Cap :
u32 { kCapDepthTest, kCapStencilTest, kCapCull, kCapBlend, kCapCount };
272 i8 cap_state_[kCapCount] = {-1, -1, -1, -1};
274 i16 color_mask_ = -1;
275 i64 stencil_mask_ = -1;
277 struct UniformBound {
u32 name = 0;
u32 offset = 0;
u32 size = 0;
bool known =
false; };
278 std::vector<UniformBound> uniform_bound_;
279 void setCap(Cap cap,
u32 glCap,
bool enabled);
280 void resetDrawStateCaches();
282 u32 current_pipeline_id_ = 0;
288 u32 current_program_name_ = 0;
291 std::unordered_map<u32, u32> sampler_units_;
293 bool current_blend_adds_only_ =
false;
298 static constexpr u32 kTextureSlots = 17;
299 u32 active_texture_unit_ = 0;
300 u32 bound_texture_[kTextureSlots] = {};
301 i16 current_depth_bias_ = 0;
302 i8 depth_equal_ = -1;
303 i8 alpha_to_coverage_ = -1;
308 bool current_fbo_has_gbuffer_ =
false;
309 bool current_fbo_has_albedo_ =
false;
310 i8 draw_buffers_ = -1;
311 void applyDrawBuffers(
bool gbuffer,
bool albedo);
313 i8 indexed_blend_ = 0;
315 bool surface_blend_off_ =
false;
316 void applySurfaceBlend(
bool off);
317 int scissor_test_ = -1;
318 int float_targets_ = -1;
322 bool configured =
false;
335 bool operator==(
const VaoKey&)
const =
default;
338 usize operator()(
const VaoKey& key)
const {
344 std::unordered_map<VaoKey, VaoCache, VaoKeyHash> vaos_;
347 VaoKey last_vao_key_{};
348 VaoCache* last_vao_ =
nullptr;
350 void releaseVaosUsing(
u32 bufferName);
351 u32 current_layout_ = 0;
354 u32 pending_ibo_ = 0;
357 bool multi_draw_ =
false;
358 bool loop_floor_ =
false;
359 std::vector<i32> multi_counts_, multi_instances_;
360 std::vector<const void*> multi_offsets_;
370 bool gbuffer =
false;
373 std::unordered_map<u32, ResolvePair> framebuffer_resolve_;
376 void resolveFramebuffer(
u32 framebufferId);
378 u32 current_framebuffer_ = 0;
380 u32 max_samples_ = 0;
386 std::vector<u8> pixels;
388 void* fence =
nullptr;
391 std::unordered_map<u32, Readback> readbacks_;
393 void releaseReadback(Readback& readback,
bool live);
394 u32 next_readback_id_ = 1;
397 int timer_query_state_ = 0;
398 bool timer_disjoint_pending_ =
false;
400 void* frame_fence_ =
nullptr;
Abstract graphics device interface.
void init() override
Initializes the graphics device.
bool backendCreateBuffer(u32 id, const BufferDesc &desc, const void *data) override
bool supportsUnblendedSurfaceTarget() const override
Whether a blended draw can write the surface record's second attachment unblended.
void backendDeleteFramebuffer(u32 id) override
~GLDevice() override=default
void setViewport(i32 x, i32 y, u32 w, u32 h) override
Sets the rendering viewport.
bool framePending() override
Whether the GPU is still working on the frame last ended (see endFrame).
void backendUniformBlockBinding(u32 program, u32 nativeBlockIndex, u32 bindingPoint) override
u32 backendReadbackCount() const override
Readbacks in flight, the one live object the registry does not hold.
void backendSetUniform(i32 nativeLocation, const GfxUniformValue &value) override
void beginTimerQuery(u32 query) override
Starts timing GPU work into a query; one query may be active at a time.
void discardReadback(ReadbackHandle handle) override
Abandons a readback (any state); its resources are released.
ClipDepthRange clipDepthRange() const override
The clip volume this device keeps along z.
void backendDeleteBuffer(u32 id) override
void backendUseProgram(u32 id) override
std::vector< GfxUniformInfo > backendActiveUniforms(u32 program) override
bool resolvesDepth() const override
bool recreateDevice() override
Backend half of recoverDevice: a working device again, with every id-to-native map and derived cache ...
void backendInvalidatePipelineCache() override
void backendIntUniformSet(u32 program) override
void onDeviceLost() override
Release what only this backend can name, while the dead device is still the current one.
void endRenderPass() override
Ends the current pass, restoring the default framebuffer.
void backendDeleteProgram(u32 id) override
void setIndexBuffer(BufferHandle buffer) override
Binds the index buffer for subsequent indexed draws.
void backendResizeBuffer(u32 id, const BufferDesc &desc, const void *data) override
void backendDeleteTexture(u32 id) override
i32 getInt(GfxIntParam name) override
Queries a backend integer capability/limit.
u32 maxSamples() override
Most samples an offscreen colour attachment may carry; 1 = none.
void backendUpdateBuffer(u32 id, u32 offsetBytes, const void *data, u32 sizeBytes) override
bool backendProgramPending(u32 id) const override
True while a program created with deferLink has not been waited for.
bool backendCreateTexture(u32 id, const TextureDesc &desc, const void *pixels) override
u32 nativeTextureName(TextureHandle texture) const override
This generation's GL name behind a texture, for a host uploading into it.
bool supportsCompressedFormat(GfxCompressedFormat format) override
Reports whether the backend can sample the given compressed format.
bool backendCreateTimerQuery(u32 id) override
bool timerDisjoint() override
True when GPU timing was disturbed since the last check.
void setStencilReference(i32 ref) override
GL reads a framebuffer from the bottom up.
void drawElementsInstanced(u32 indexCount, GfxDataType indexType, u32 byteOffset, u32 instanceCount) override
Draws indexed triangles with instancing.
void backendGenerateMipmaps(u32 id) override
bool supportsShaderLanguage(GfxShaderLanguage language) const override
Whether this backend compiles language (GL: GLSL ES 300; WebGPU: WGSL).
void drawElements(u32 indexCount, GfxDataType indexType, u32 byteOffset) override
Draws indexed triangles.
void captureDeviceIdentity() override
Records who this backend is, so a later loss report can name it.
bool takeReadback(ReadbackHandle handle, void *dest, usize destSize) override
Copies a Ready readback's pixels (w*h*4 bytes) into dest and releases it.
void clearStencil(i32 value) override
Mid-pass stencil reset (the one clear that isn't a pass load-op).
u32 primitiveMode() const
bool backendCreateProgram(u32 id, const GfxShaderSource &source, const GfxAttribBinding *bindings, u32 bindingCount, std::string *outLog, GfxShaderStage *outFailedStage) override
void endTimerQuery() override
Stops the active timer query.
bool supportsDeferredLink() const override
Whether createProgram honours GfxShaderSource::deferLink (GL: yes; a backend that links at once does ...
ReadbackHandle requestReadback(FramebufferHandle target, u32 w, u32 h) override
Requests an RGBA8 readback of a framebuffer's color attachment.
u32 backendUniformBlockIndex(u32 program, const char *name) override
void shutdown() override
Shuts down the graphics device.
void backendSetTextureParams(u32 id, const TextureDesc &desc) override
void setWireframe(bool enabled) override
Enables or disables wireframe rendering (desktop only).
bool backendCreateFramebuffer(u32 id, const FramebufferDesc &desc) override
bool backendAdoptTexture(u32 id, u32 nativeId, const TextureDesc &desc) override
Takes over a native texture the host created; false when this backend cannot.
void drawElementsInstancedMulti(GfxDataType indexType, const u32 *counts, const u32 *byteOffsets, const u32 *instanceCounts, u32 drawCount) override
drawCount indexed instanced draws through the bound pipeline and buffers, as one call where supportsM...
bool backendFinishProgram(u32 id) override
Waits for a deferred program, runs its onLinked callback; false when it failed to link....
GfxReadbackStatus pollReadback(ReadbackHandle handle) override
The state of an in-flight readback; pumps backend completion events. A Failed readback is released by...
void backendUpdateTexture(u32 id, i32 x, i32 y, u32 width, u32 height, const void *pixels, bool flipY) override
bool supportsMultiDraw() const override
Whether drawElementsInstancedMulti is one call here, its draws told apart by gl_DrawID (WEBGL_multi_d...
std::string backendPendingProgramLabels() override
bool getTimerQueryNs(u32 query, u64 *outNanoseconds) override
Fetches a completed query's elapsed nanoseconds; false while still pending.
void backendMoveTexture(u32 into, u32 from) override
Re-keys from's native texture under into; into holds none on entry.
void backendDeleteVertexLayout(u32 id) override
Drops what the backend derived from a layout (a GL VAO); the desc stays here.
bool supportsFloatTargets() override
Whether RGBA16F color attachments are renderable.
u32 backendPendingProgramCount() override
u32 backendBoundSamplerUnits() override
void bindTexture(u32 slot, TextureHandle texture) override
Activates a texture slot and binds a 2D texture.
void beginRenderPass(const RenderPassDesc &desc) override
Targets a framebuffer for subsequent draws and applies the pass's clears.
void drawArrays(u32 first, u32 vertexCount) override
Draws non-indexed triangles.
std::string getString(GfxStringName name) override
Queries a backend identification string (diagnostics).
ReadbackHandle captureNextFrame(u32 w, u32 h) override
void backendSetUniformBuffer(u32 slot, BufferHandle buffer, u32 offsetBytes, u32 sizeBytes) override
void backendSetPipeline(u32 id, const PipelineDesc &desc) override
bool backendCreateCompressedTexture(u32 id, const TextureDesc &desc, GfxCompressedFormat format, const void *data, u32 byteLength, u32 mipLevels) override
void setScissor(i32 x, i32 y, i32 w, i32 h) override
Sets the scissor rectangle.
bool backendProgramReady(u32 id) override
void endFrame() override
The frame is over — everything drawn for it has been submitted.
bool pollDeviceLost() override
Pumps backend-native loss detection.
bool copyFramebufferColor(FramebufferHandle source, TextureHandle dst) override
i32 backendAttribLocation(u32 program, const char *name) override
void setVertexBuffer(u32 slot, BufferHandle buffer, u32 offsetBytes) override
Binds a vertex buffer to a layout slot for subsequent draws.
u32 getError() override
Queries the last error.
i32 backendUniformLocation(u32 program, const char *name) override
The backend's own location this generation, -1 when absent.
void setScissorTest(bool enabled) override
Enables or disables scissor test.
Abstract graphics device interface.
constexpr u64 fnv1aWord(u64 h, u64 word)
constexpr u64 kFnv64Offset
FramebufferHandle
Handle 0 is the default framebuffer (backbuffer); it cannot be created or deleted.
std::int32_t i32
Signed 32-bit integer.
GfxStringName
Backend identification strings.
std::uint64_t u64
Unsigned 64-bit integer.
ClipDepthRange
What a device's clip volume does with depth.
@ MinusOneToOne
OpenGL / OpenGL ES / WebGL.
GfxStencilMode
Stencil behaviour baked into a pipeline. The reference value is dynamic (GfxDevice::setStencilReferen...
@ Off
No stencil test; full color write.
std::size_t usize
Size type for indexing and sizes.
std::int16_t i16
Signed 16-bit integer.
GfxShaderLanguage
Shader source language a backend consumes.
GfxShaderStage
Which pipeline stage rejected a program during createProgram().
GfxCompressedFormat
GPU-compressed texture internal formats for createCompressedTexture.
std::int8_t i8
Signed 8-bit integer.
GfxReadbackStatus
State of an in-flight readback. GL completes at request time (first poll reports Ready); WebGPU resol...
std::uint32_t u32
Unsigned 32-bit integer.
BlendMode
Predefined blend modes for rendering.
std::int64_t i64
Signed 64-bit integer.
static constexpr u32 MAX_VERTEX_BUFFER_SLOTS
GfxIntParam
Backend integer capabilities/limits.
GfxTopology
How a pipeline's indices assemble into primitives. Baked into the pipeline, as WebGPU bakes it,...
@ Triangles
Every three indices a triangle: all geometry the engine draws.
ReadbackHandle
An in-flight framebuffer readback (GfxDevice::requestReadback).
float f32
32-bit floating point
Immutable creation parameters of a GPU buffer.
Attachments of an offscreen render target. The attach point of depthStencil follows its texture's pix...
Vertex attribute location binding applied before a program is linked.
A program's source pair, tagged with the language it is written in.
Immutable description of a draw pipeline. Equality keys the pipeline cache, so two draws with the sam...
One render pass: a target, which attachments to clear on entry, and the clear values — full load-op s...
Immutable creation parameters of a 2D texture or 2D texture array.