shared.h

cross platform rendering playground

src/shaders/shared.h

2.16 KB
#pragma once

#ifdef __SLANG__

typealias f32 = float;
typealias f64 = double;

typealias vec2 = float2;
typealias vec3 = float3;
typealias vec4 = float4;
typealias mat3 = float3x3;
typealias mat4 = float4x4;

typealias u32 = uint32_t;
typealias u64 = uint64_t;

typealias uvec2 = uint2;
typealias uvec3 = uint3;
typealias uvec4 = uint4;

typealias i32 = int32_t;
typealias i64 = int64_t;

typealias ivec2 = int2;
typealias ivec3 = int3;
typealias ivec4 = int4;

typealias bvec2 = bool2;
typealias bvec3 = bool3;
typealias bvec4 = bool4;

float4 operator*(float4 vec, float4x4 matrix) {
    return mul(vec, matrix);
}

float3 operator*(float3 vec, float3x3 matrix) {
    return mul(vec, matrix);
}

float4x4 operator*(float4x4 matrixLeft, float4x4 matrixRight) {
    return mul(matrixLeft, matrixRight);
}

#define GPU_PTR(T)   T *
#define GPU_HND(T)   T.Handle
#define STATIC_CONST static const

#else

#include "core/globals.h"
#include "core/math_rtm.h"

#define GPU_PTR(type) u64
#define GPU_HND(type) u64
#define STATIC_CONST  const

#endif

STATIC_CONST int VK_DESCRIPTOR_SET = 0;
STATIC_CONST int VK_MUTABLE_BINDING = 0;
STATIC_CONST int VK_SAMPLER_BINDING = 1;

#ifdef __SLANG__
[[vk::binding(VK_MUTABLE_BINDING, VK_DESCRIPTOR_SET)]]
__DynamicResource<__DynamicResourceKind.General> vk_mutable_set[];
[[vk::binding(VK_SAMPLER_BINDING, VK_DESCRIPTOR_SET)]]
__DynamicResource<__DynamicResourceKind.Sampler> vk_sampler_set[];

export T getDescriptorFromHandle<T>(DescriptorHandle<T> handle) where T : IOpaqueDescriptor {
    __target_switch {
    case spirv:
        if (T.kind == DescriptorKind.Sampler) {
            return vk_sampler_set[((uint2)handle).x].asOpaqueDescriptor<T>(); // 32-bit OpAccessChain
        } else {
            return vk_mutable_set[((uint2)handle).x].asOpaqueDescriptor<T>(); // 32-bit OpAccessChain
        }
    default:
        return defaultGetDescriptorFromHandle(handle);
    }
}

// nuked FetchConstant(), but the underlying issue persists using * workaround to deref for now
// https://github.com/shader-slang/slang/pull/11685
// https://github.com/shader-slang/slang/issues/11681
// https://github.com/shader-slang/slang/issues/11681#issuecomment-4896244646
#endif