instancing

This commit is contained in:
2024-08-09 21:16:07 +10:00
parent 89780d87d4
commit 5f69da369d
3 changed files with 36 additions and 21 deletions

View File

@ -1,25 +1,28 @@
#include "shadertypes.h"
#include <metal_stdlib>
using namespace metal;
struct FragmentInput {
float4 position [[position]];
float4 normal;
float2 texCoord;
half4 color;
};
vertex FragmentInput vertexMain(
uint vertexID [[vertex_id]],
uint instanceID [[instance_id]],
device const ShaderVertex* vtx [[buffer(ShaderInputIdxVertices)]],
device const ShaderInstance* i [[buffer(ShaderInputIdxInstance)]],
constant ShaderUniforms& u [[buffer(ShaderInputIdxUniforms)]]
) {
auto position = vtx[vertexID].position;
position = u.projView * u.model * position;
auto world = i[instanceID].model * position;
auto ndc = u.projView * world;
FragmentInput out;
out.position = position;
out.position = ndc;
out.color = half4(i[instanceID].color);
out.normal = vtx[vertexID].normal;
out.texCoord = vtx[vertexID].texCoord;
return out;
@ -27,10 +30,9 @@ vertex FragmentInput vertexMain(
fragment half4 fragmentMain(
FragmentInput in [[stage_in]],
texture2d<half, access::sample> tex [[texture(0)]]
metal::texture2d<half, metal::access::sample> texture [[texture(0)]]
) {
constexpr sampler s(address::repeat, filter::nearest);
half4 albedo = tex.sample(s, in.texCoord);
return half4(albedo.rgb, 1.0);
constexpr metal::sampler sampler(metal::address::repeat, metal::filter::nearest);
half4 albedo = texture.sample(sampler, in.texCoord);
return albedo * in.color;
}