summaryrefslogtreecommitdiff
path: root/src/gpu/sampler_cache.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/sampler_cache.cc')
-rw-r--r--src/gpu/sampler_cache.cc32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/gpu/sampler_cache.cc b/src/gpu/sampler_cache.cc
new file mode 100644
index 0000000..c61c232
--- /dev/null
+++ b/src/gpu/sampler_cache.cc
@@ -0,0 +1,32 @@
+// Sampler cache implementation
+// See sampler_cache.h
+#include "gpu/sampler_cache.h"
+
+#include "platform/platform.h"
+#include "util/fatal_error.h"
+
+WGPUSampler SamplerCache::get_or_create(WGPUDevice device,
+ const SamplerSpec& spec) {
+ HEADLESS_RETURN_VAL_IF_NULL(device, nullptr);
+
+ auto it = cache_.find(spec);
+ if (it != cache_.end())
+ return it->second;
+
+ WGPUSamplerDescriptor desc{};
+ desc.addressModeU = spec.u;
+ desc.addressModeV = spec.v;
+ desc.magFilter = spec.mag;
+ desc.minFilter = spec.min;
+ desc.maxAnisotropy = spec.anisotropy;
+ WGPUSampler sampler = wgpuDeviceCreateSampler(device, &desc);
+ cache_[spec] = sampler;
+ return sampler;
+}
+
+void SamplerCache::clear() {
+ for (auto& pair : cache_) {
+ wgpuSamplerRelease(pair.second);
+ }
+ cache_.clear();
+}