gboisse / gfx

A minimalist and easy to use graphics API.
MIT License
498 stars 35 forks source link

Providing correct types for sharing constant buffer structures between C++ and HLSL #51

Closed smeunier-amd closed 11 months ago

smeunier-amd commented 2 years ago

A common pattern for implementing constant buffer is to share a header between C++ and HLSL :

#ifndef GPU_SHARED_H
#define GPU_SHARED_H

#ifdef __cplusplus

#include <glm/gtx/type_aligned.hpp>
#include <glm/gtx/compatibility.hpp>

typedef uint32_t uint;

typedef glm::ivec2 int2;
typedef glm::ivec4 int4;

typedef glm::uvec2 uint2;
typedef glm::aligned_uvec3 uint3;
typedef glm::uvec4 uint4;

typedef glm::aligned_vec4 float4;
typedef glm::aligned_vec3 float3;
typedef glm::aligned_vec2 float2;

typedef glm::bool3 bool3;

typedef glm::mat4 float4x4;

#endif // __cplusplus

enum MyEnum
{
    VALUE1 = 0,
    VALUE2
};

struct MyContants
{
    float  attribute0;
    uint   attribute1;
    uint2  attribute2;
    uint   attribute3;
    uint   attribute4;
    MyEnum attribute5;
};

#endif // GPU_SHARED_H

These typedef declarations are probably wrong :

HLSL contant buffer alignment rules aren't obvious, and we still can't use -not_use_legacy_cbuf_load with DXC.

I propose gfx provide a small set (subset?) of types we can use and arrange freely in constant buffers.

gboisse commented 11 months ago

Closing due to inactivity.