| 1 |
// Copyright 2022 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "VideoBackends/Metal/MTLPipeline.h" |
| 5 |
|
| 6 |
#include "Common/MsgHandler.h" |
| 7 |
|
| 8 |
static void MarkAsUsed(u32* list, u32 start, u32 length) |
| 9 |
{ |
| 10 |
for (u32 i = start; i < start + length; ++i) |
| 11 |
*list |= 1 << i; |
| 12 |
} |
| 13 |
|
| 14 |
static void GetArguments(NSArray<MTLArgument*>* arguments, u32* textures, u32* samplers, |
| 15 |
u32* buffers) |
| 16 |
{ |
| 17 |
for (MTLArgument* argument in arguments) |
| 18 |
{ |
| 19 |
const u32 idx = [argument index]; |
| 20 |
const u32 length = [argument arrayLength]; |
| 21 |
if (idx + length > 32) |
| 22 |
{ |
| 23 |
PanicAlertFmt("Making a MTLPipeline with high argument index {:d}..<{:d} for {:s}", // |
| 24 |
idx, idx + length, [[argument name] UTF8String]); |
| 25 |
continue; |
| 26 |
} |
| 27 |
switch ([argument type]) |
| 28 |
{ |
| 29 |
case MTLArgumentTypeTexture: |
| 30 |
if (textures) |
| 31 |
MarkAsUsed(textures, idx, length); |
| 32 |
else |
| 33 |
PanicAlertFmt("Vertex function wants a texture!"); |
| 34 |
break; |
| 35 |
case MTLArgumentTypeSampler: |
| 36 |
if (samplers) |
| 37 |
MarkAsUsed(samplers, idx, length); |
| 38 |
else |
| 39 |
PanicAlertFmt("Vertex function wants a sampler!"); |
| 40 |
break; |
| 41 |
case MTLArgumentTypeBuffer: |
| 42 |
MarkAsUsed(buffers, idx, length); |
| 43 |
break; |
| 44 |
default: |
| 45 |
break; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
Metal::PipelineReflection::PipelineReflection(MTLRenderPipelineReflection* reflection) |
| 51 |
{ |
| 52 |
GetArguments([reflection vertexArguments], nullptr, nullptr, &vertex_buffers); |
| 53 |
GetArguments([reflection fragmentArguments], &textures, &samplers, &fragment_buffers); |
| 54 |
} |
| 55 |
|
| 56 |
Metal::Pipeline::Pipeline(const AbstractPipelineConfig& config, |
| 57 |
MRCOwned<id<MTLRenderPipelineState>> pipeline, |
| 58 |
const PipelineReflection& reflection, MTLPrimitiveType prim, |
| 59 |
MTLCullMode cull, DepthState depth, AbstractPipelineUsage usage) |
| 60 |
: AbstractPipeline(config), m_pipeline(std::move(pipeline)), m_prim(prim), m_cull(cull), |
| 61 |
m_depth_stencil(depth), m_usage(usage), m_reflection(reflection) |
| 62 |
{ |
| 63 |
} |
| 64 |
|
| 65 |
Metal::ComputePipeline::ComputePipeline(ShaderStage stage, MTLComputePipelineReflection* reflection, |
| 66 |
std::string msl, MRCOwned<id<MTLFunction>> shader, |
| 67 |
MRCOwned<id<MTLComputePipelineState>> pipeline) |
| 68 |
: Shader(stage, std::move(msl), std::move(shader)), m_compute_pipeline(std::move(pipeline)) |
| 69 |
{ |
| 70 |
GetArguments([reflection arguments], &m_textures, &m_samplers, &m_buffers); |
| 71 |
} |