VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
renderPassUpsample.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4//
6
9
10namespace visutwin::canvas
11{
12 namespace
13 {
14 constexpr const char* UPSAMPLE_SOURCE = R"(
15#include <metal_stdlib>
16using namespace metal;
17
18struct QuadVertexIn {
19 float3 position [[attribute(0)]];
20 float3 normal [[attribute(1)]];
21 float2 uv0 [[attribute(2)]];
22 float4 tangent [[attribute(3)]];
23 float2 uv1 [[attribute(4)]];
24};
25
26struct QuadVarying {
27 float4 position [[position]];
28 float2 uv;
29};
30
31vertex QuadVarying upsampleVertex(QuadVertexIn in [[stage_in]])
32{
33 QuadVarying out;
34 out.position = float4(in.position, 1.0);
35 out.uv = in.uv0;
36 return out;
37}
38
39fragment float4 upsampleFragment(
40 QuadVarying in [[stage_in]],
41 texture2d<float> sourceTexture [[texture(0)]],
42 sampler linearSampler [[sampler(0)]])
43{
44 return sourceTexture.sample(linearSampler, clamp(in.uv, float2(0.0), float2(1.0)));
45}
46)";
47 }
48
49 RenderPassUpsample::RenderPassUpsample(const std::shared_ptr<GraphicsDevice>& device, Texture* sourceTexture)
50 : RenderPassShaderQuad(device), _sourceTexture(sourceTexture)
51 {
52 // Cache the upsample shader at the device level (same rationale as downsample).
53 static constexpr const char* CACHE_KEY = "UpsampleQuad";
54 auto cached = device->getCachedShader(CACHE_KEY);
55 if (!cached) {
56 ShaderDefinition shaderDefinition;
57 shaderDefinition.name = CACHE_KEY;
58 shaderDefinition.vshader = "upsampleVertex";
59 shaderDefinition.fshader = "upsampleFragment";
60 cached = createShader(device.get(), shaderDefinition, UPSAMPLE_SOURCE);
61 device->setCachedShader(CACHE_KEY, cached);
62 }
63 setShader(cached);
64 }
65
71}
std::shared_ptr< GraphicsDevice > device() const
Definition renderPass.h:124
void setShader(const std::shared_ptr< Shader > &shader)
void setQuadTextureBinding(const size_t slot, Texture *texture)
RenderPassShaderQuad(const std::shared_ptr< GraphicsDevice > &device)
RenderPassUpsample(const std::shared_ptr< GraphicsDevice > &device, Texture *sourceTexture)
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57
std::shared_ptr< Shader > createShader(GraphicsDevice *graphicsDevice, const ShaderDefinition &definition, const std::string &sourceCode)
Definition shader.cpp:39