stringify utility shader source

This commit is contained in:
Samuliak 2024-08-14 16:21:43 +02:00
parent d48de5b56f
commit c4688e1ad1
2 changed files with 63 additions and 51 deletions

View File

@ -57,9 +57,17 @@ MetalRenderer::MetalRenderer()
} }
} }
// Utility shader source // Utility shader library
// Process the source first
std::string processedUtilityShaderSource = utilityShaderSource;
processedUtilityShaderSource.pop_back();
processedUtilityShaderSource.erase(processedUtilityShaderSource.begin());
processedUtilityShaderSource = "#include <metal_stdlib>\n" + processedUtilityShaderSource;
// Create the library
NS::Error* error = nullptr; NS::Error* error = nullptr;
MTL::Library* utilityLibrary = m_device->newLibrary(NS::String::string(utilityShaderSource, NS::ASCIIStringEncoding), nullptr, &error); MTL::Library* utilityLibrary = m_device->newLibrary(NS::String::string(processedUtilityShaderSource.c_str(), NS::ASCIIStringEncoding), nullptr, &error);
if (error) if (error)
{ {
debug_printf("failed to create utility library (error: %s)\n", error->localizedDescription()->utf8String()); debug_printf("failed to create utility library (error: %s)\n", error->localizedDescription()->utf8String());

View File

@ -1,49 +1,53 @@
inline const char* utilityShaderSource = \ #pragma once
"#include <metal_stdlib>\n" \
"using namespace metal;\n" \ #define __STRINGIFY(x) #x
"\n" \ #define _STRINGIFY(x) __STRINGIFY(x)
"constant float2 positions[] = {float2(-1.0, -3.0), float2(-1.0, 1.0), float2(3.0, 1.0)};\n"
"\n" \ constexpr const char* utilityShaderSource = _STRINGIFY((
"struct VertexOut {\n" \ using namespace metal;
" float4 position [[position]];\n" \
" float2 texCoord;\n" \ constant float2 positions[] = {float2(-1.0, -3.0), float2(-1.0, 1.0), float2(3.0, 1.0)};
"};\n" \
"\n" \ struct VertexOut {
"vertex VertexOut vertexFullscreen(ushort vid [[vertex_id]]) {\n" \ float4 position [[position]];
" VertexOut out;\n" \ float2 texCoord;
" out.position = float4(positions[vid], 0.0, 1.0);\n" \ };
" out.texCoord = positions[vid] * 0.5 + 0.5;\n" \
" out.texCoord.y = 1.0 - out.texCoord.y;\n" \ vertex VertexOut vertexFullscreen(ushort vid [[vertex_id]]) {
"\n" \ VertexOut out;
" return out;\n" \ out.position = float4(positions[vid], 0.0, 1.0);
"}\n" \ out.texCoord = positions[vid] * 0.5 + 0.5;
"\n" \ out.texCoord.y = 1.0 - out.texCoord.y;
"fragment float4 fragmentPresent(VertexOut in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler samplr [[sampler(0)]]) {\n" \
" return tex.sample(samplr, in.texCoord);\n" \ return out;
"}\n" \ }
"\n" \
"struct CopyParams {\n" \ fragment float4 fragmentPresent(VertexOut in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler samplr [[sampler(0)]]) {
" uint width;\n" \ return tex.sample(samplr, in.texCoord);
" uint srcMip;\n" \ }
" uint srcSlice;\n" \
" uint dstMip;\n" \ struct CopyParams {
" uint dstSlice;\n" \ uint width;
"};\n" \ uint srcMip;
"\n" \ uint srcSlice;
"vertex void vertexCopyTextureToTexture(uint vid [[vertex_id]], texture2d_array<float, access::read> src [[texture(0)]], texture2d_array<float, access::write> dst [[texture(1)]], constant CopyParams& params [[buffer(0)]]) {\n" \ uint dstMip;
" uint2 coord = uint2(vid % params.width, vid / params.width);\n" \ uint dstSlice;
" return dst.write(float4(src.read(coord, params.srcSlice, params.srcMip).r, 0.0, 0.0, 0.0), coord, params.dstSlice, params.dstMip);\n" \ };
"}\n" \
"\n" \ vertex void vertexCopyTextureToTexture(uint vid [[vertex_id]], texture2d_array<float, access::read> src [[texture(0)]], texture2d_array<float, access::write> dst [[texture(1)]], constant CopyParams& params [[buffer(0)]]) {
"struct RestrideParams {\n" \ uint2 coord = uint2(vid % params.width, vid / params.width);
" uint oldStride;\n" \ return dst.write(float4(src.read(coord, params.srcSlice, params.srcMip).r, 0.0, 0.0, 0.0), coord, params.dstSlice, params.dstMip);
" uint newStride;\n" \ }
"};\n" \
"\n" \ struct RestrideParams {
/* TODO: use uint32? Since that would require less iterations */ \ uint oldStride;
"vertex void vertexRestrideBuffer(uint vid [[vertex_id]], device uint8_t* src [[buffer(0)]], device uint8_t* dst [[buffer(1)]], constant RestrideParams& params [[buffer(2)]]) {\n" \ uint newStride;
" for (uint32_t i = 0; i < params.oldStride; i++) {\n" \ };
" dst[vid * params.newStride + i] = src[vid * params.oldStride + i];\n" \
" }\n" \ /* TODO: use uint32? Since that would require less iterations */
"}\n" \ vertex void vertexRestrideBuffer(uint vid [[vertex_id]], device uint8_t* src [[buffer(0)]], device uint8_t* dst [[buffer(1)]], constant RestrideParams& params [[buffer(2)]]) {
"\n"; for (uint32_t i = 0; i < params.oldStride; i++) {
dst[vid * params.newStride + i] = src[vid * params.oldStride + i];
}
}
));