summaryrefslogtreecommitdiff
path: root/src/gpu/sampler_cache.h
blob: 0a2afa08da6cf69c0be9508f08b0ad475c158f21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Sampler cache - deduplicates WGPUSampler objects across effects
// Single entry point: SamplerCache::Get().get_or_create(device, spec)
#pragma once
#include <map>

// Forward declarations (users must include gpu.h)
struct WGPUDeviceImpl;
typedef struct WGPUDeviceImpl* WGPUDevice;
struct WGPUSamplerImpl;
typedef struct WGPUSamplerImpl* WGPUSampler;

#include "platform/platform.h"

struct SamplerSpec {
  WGPUAddressMode u, v;
  WGPUFilterMode mag, min;
  uint16_t anisotropy;

  bool operator<(const SamplerSpec& o) const {
    if (u != o.u)
      return u < o.u;
    if (v != o.v)
      return v < o.v;
    if (mag != o.mag)
      return mag < o.mag;
    if (min != o.min)
      return min < o.min;
    return anisotropy < o.anisotropy;
  }
};

class SamplerCache {
  std::map<SamplerSpec, WGPUSampler> cache_;
  SamplerCache() = default;

 public:
  static SamplerCache& Get() {
    static SamplerCache instance;
    return instance;
  }

  WGPUSampler get_or_create(WGPUDevice device, const SamplerSpec& spec);
  void clear();

  // Common presets
  static SamplerSpec linear() {
    return {WGPUAddressMode_Repeat, WGPUAddressMode_Repeat,
            WGPUFilterMode_Linear, WGPUFilterMode_Linear, 1};
  }
  static SamplerSpec clamp() {
    return {WGPUAddressMode_ClampToEdge, WGPUAddressMode_ClampToEdge,
            WGPUFilterMode_Linear, WGPUFilterMode_Linear, 1};
  }
};