VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
metalTextureBinder.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Per-pass texture binding deduplication.
5// Extracted from MetalGraphicsDevice for single-responsibility decomposition.
6//
7#pragma once
8
9#include <array>
10#include <vector>
11#include <Metal/Metal.hpp>
12
13namespace visutwin::canvas
14{
15 class Texture;
16 struct TextureSlot;
17
25 {
26 public:
27 static constexpr int kMaxTextureSlots = 19; // Slots 0-18; 11-12 = spot shadow, 15-16 = omni shadow cubemaps, 17 = height map, 18 = SSAO
28
30 void bindCached(MTL::RenderCommandEncoder* encoder, int slot, Texture* texture);
31
33 void clearCached(MTL::RenderCommandEncoder* encoder, int slot);
34
36 void bindSamplerCached(MTL::RenderCommandEncoder* encoder, MTL::SamplerState* sampler);
37
40 void bindMaterialTextures(MTL::RenderCommandEncoder* encoder,
41 const std::vector<TextureSlot>& textureSlots);
42
44 void clearAllMaterialSlots(MTL::RenderCommandEncoder* encoder);
45
47 void bindSceneTextures(MTL::RenderCommandEncoder* encoder,
48 Texture* envAtlas, Texture* shadow, Texture* sceneDepth, Texture* skyboxCubeMap,
49 Texture* reflection = nullptr, Texture* reflectionDepth = nullptr,
50 Texture* ssao = nullptr);
51
53 void bindQuadTextures(MTL::RenderCommandEncoder* encoder,
54 const std::array<Texture*, 8>& quadBindings);
55
57 void bindLocalShadowTextures(MTL::RenderCommandEncoder* encoder,
58 Texture* shadow0, Texture* shadow1);
59
61 void bindOmniShadowTextures(MTL::RenderCommandEncoder* encoder,
62 Texture* cube0, Texture* cube1);
63
65 void markClean() { _dirty = false; }
66
68 void resetPassState();
69
70 private:
71 std::array<Texture*, kMaxTextureSlots> _boundTextures{};
72 bool _dirty = true;
73 MTL::SamplerState* _boundSampler = nullptr;
74 };
75}
void bindSamplerCached(MTL::RenderCommandEncoder *encoder, MTL::SamplerState *sampler)
Bind a sampler at slot 0, skipping if already bound.
void bindSceneTextures(MTL::RenderCommandEncoder *encoder, Texture *envAtlas, Texture *shadow, Texture *sceneDepth, Texture *skyboxCubeMap, Texture *reflection=nullptr, Texture *reflectionDepth=nullptr, Texture *ssao=nullptr)
Bind scene-global textures (envAtlas, shadow, sceneDepth, skybox cubemap, reflection,...
void resetPassState()
Reset all cached state. Must be called at the start of each render pass.
void clearCached(MTL::RenderCommandEncoder *encoder, int slot)
Clear (unbind) a texture slot, skipping if already nullptr.
void bindCached(MTL::RenderCommandEncoder *encoder, int slot, Texture *texture)
Bind a texture at the given fragment slot, skipping if already bound.
void clearAllMaterialSlots(MTL::RenderCommandEncoder *encoder)
Clear all 8 texture slots (used when no material is bound).
void markClean()
Mark the cache as clean after the first draw in a pass.
void bindOmniShadowTextures(MTL::RenderCommandEncoder *encoder, Texture *cube0, Texture *cube1)
Bind omni shadow cubemap depth textures at slots 15 and 16 (point lights, cube).
void bindLocalShadowTextures(MTL::RenderCommandEncoder *encoder, Texture *shadow0, Texture *shadow1)
Bind local shadow depth textures at slots 11 and 12 (spot lights, 2D).
void bindMaterialTextures(MTL::RenderCommandEncoder *encoder, const std::vector< TextureSlot > &textureSlots)
void bindQuadTextures(MTL::RenderCommandEncoder *encoder, const std::array< Texture *, 8 > &quadBindings)
Bind quad render textures for all 8 slots.
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57