Skip to content

Commit

Permalink
Big progress on bindless resource usage. Need another pass on the mes…
Browse files Browse the repository at this point in the history
…h system but it's an improvement
  • Loading branch information
Honeybunch committed Dec 13, 2023
1 parent 077b79f commit 9f53fc2
Show file tree
Hide file tree
Showing 14 changed files with 538 additions and 524 deletions.
1 change: 1 addition & 0 deletions include/common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ TbCommonLightData;
typedef struct TB_GPU_STRUCT TbCommonObjectData {
float4x4 m;
int32_t perm; // Input layout permutation
int32_t mat_idx;
}
TbCommonObjectData;

Expand Down
52 changes: 20 additions & 32 deletions include/gltf.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,26 @@
#define GLTF_PERM_ALPHA_BLEND 0x00002000
#define GLTF_PERM_DOUBLE_SIDED 0x00004000

#ifdef __HLSL_VERSION
#define PACKED
#else
#define PACKED __attribute__((__packed__))
#endif

// Omitting texture rotation because it's not widely used
typedef struct PACKED TextureTransform {
typedef struct TB_GPU_STRUCT TextureTransform {
float2 offset;
float2 scale;
}
TextureTransform;

typedef struct PACKED PBRMetallicRoughness {
typedef struct TB_GPU_STRUCT PBRMetallicRoughness {
float4 base_color_factor;
float4 metal_rough_factors;
}
PBRMetallicRoughness;

typedef struct PACKED PBRSpecularGlossiness {
typedef struct TB_GPU_STRUCT PBRSpecularGlossiness {
float4 diffuse_factor;
float4 spec_gloss_factors;
}
PBRSpecularGlossiness;

typedef struct PACKED GLTFMaterialData {
typedef struct TB_GPU_STRUCT GLTFMaterialData {
TextureTransform tex_transform;
PBRMetallicRoughness pbr_metallic_roughness;
PBRSpecularGlossiness pbr_specular_glossiness;
Expand All @@ -52,15 +46,19 @@ typedef struct PACKED GLTFMaterialData {
float4 attenuation_params;
float4 thickness_factor;
float4 emissives;
int32_t perm;
uint32_t color_idx;
uint32_t normal_idx;
uint32_t pbr_idx;
}
GLTFMaterialData;

#define ALPHA_CUTOFF(m) m.sheen_alpha.w

typedef struct PACKED MaterialPushConstants {
uint perm;
typedef struct GLTFPushConstants {
int32_t mat_idx;
}
MaterialPushConstants;
GLTFPushConstants;

#define ALPHA_CUTOFF(m) m.sheen_alpha.w

// If a shader, provide some helper functions and macros
#ifdef __HLSL_VERSION
Expand All @@ -72,17 +70,13 @@ float2 uv_transform(int2 quant_uv, TextureTransform trans) {
return uv;
}

#define GLTF_TEXTURE_SET(space) Texture2D gltf_textures[] : register(t0, space);

#define GLTF_MATERIAL_SET(space) \
ConstantBuffer<GLTFMaterialData> material_data : register(b0, space); \
Texture2D base_color_map : register(t1, space); \
Texture2D normal_map : register(t2, space); \
Texture2D metal_rough_map : register(t3, space); \
sampler material_sampler : register(s4, space); \
SamplerComparisonState shadow_sampler : register(s5, space); \
[[vk::push_constant]] \
ConstantBuffer<MaterialPushConstants> consts : register(b6, space);

// TODO: should probably move this somewhere outside of the GLTF concept
sampler material_sampler : register(s0, space); \
SamplerComparisonState shadow_sampler : register(s1, space); \
StructuredBuffer<GLTFMaterialData> gltf_data[] : register(t2, space);

#define GLTF_OBJECT_SET(space) \
StructuredBuffer<TbCommonObjectData> object_data : register(t0, space);

Expand All @@ -92,11 +86,9 @@ float2 uv_transform(int2 quant_uv, TextureTransform trans) {
RWBuffer<float4> tan_buffer : register(u2, space); \
RWBuffer<int2> uv0_buffer : register(u3, space);

// TODO: should probably move this somewhere outside of the GLTF concept
#define GLTF_INDIRECT_SET(space) \
StructuredBuffer<int32_t> trans_indices : register(t0, space);

// TODO: should probably move this somewhere outside of the GLTF concept
#define GLTF_VIEW_SET(space) \
ConstantBuffer<TbCommonViewData> camera_data : register(b0, space); \
TextureCube irradiance_map : register(t1, space); \
Expand Down Expand Up @@ -131,13 +123,9 @@ float2 uv_transform(int2 quant_uv, TextureTransform trans) {
s.N = normal; \
s.V = view; \
s.R = refl; \
s.emissives = material_data.emissives; \
s.emissives = gltf.emissives; \
\
out.rgb = pbr_lighting_common(v, l, s); \
out.a = color.a; \
}

#else
_Static_assert(sizeof(MaterialPushConstants) <= PUSH_CONSTANT_BYTES,
"Too Many Push Constants");
#endif
13 changes: 9 additions & 4 deletions include/materialsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "SDL2/SDL_stdinc.h"
#include "allocator.h"
#include "dynarray.h"
#include "rendersystem.h"
#include "tbcommon.h"
#include "tbrendercommon.h"

Expand All @@ -15,7 +16,7 @@ typedef struct TbTextureSystem TbTextureSystem;
typedef struct TbMaterial TbMaterial;
typedef uint64_t TbMaterialId;
typedef uint64_t TbTextureId;
typedef uint64_t TbMaterialPerm;
typedef uint32_t TbMaterialPerm;

static const TbMaterialId InvalidMaterialId = SDL_MAX_UINT64;

Expand All @@ -29,13 +30,15 @@ typedef struct TbMaterialSystem {
VkSampler sampler; // Immutable sampler for material descriptor sets
VkSampler shadow_sampler; // Immutable sampler for sampling shadow maps
VkDescriptorSetLayout set_layout;
VkDescriptorPool mat_set_pool;

const cgltf_material *default_material;

// These two arrays are to be kept in sync
TB_DYN_ARR_OF(TbMaterial) materials;
VkDescriptorSet *mat_sets;

// Descriptor pool that owns descriptor arrays of material buffer data, color,
// normal and pbr textures
TbDescriptorPool mat_pool;
} TbMaterialSystem;

void tb_register_material_sys(TbWorld *world);
Expand All @@ -48,7 +51,9 @@ TbMaterialId tb_mat_system_load_material(TbMaterialSystem *self,
const cgltf_material *material);

TbMaterialPerm tb_mat_system_get_perm(TbMaterialSystem *self, TbMaterialId mat);
VkDescriptorSet tb_mat_system_get_set(TbMaterialSystem *self, TbMaterialId mat);
uint32_t tb_mat_sys_get_idx(TbMaterialSystem *self, TbMaterialId mat);

VkDescriptorSet tb_mat_system_get_set(TbMaterialSystem *self);

void tb_mat_system_release_material_ref(TbMaterialSystem *self,
TbMaterialId mat);
11 changes: 6 additions & 5 deletions include/meshsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct TbMeshComponent TbMeshComponent;
typedef struct TbMesh TbMesh;
typedef struct TbWorld TbWorld;
typedef uint64_t TbMeshId;
typedef uint64_t TbMaterialPerm;
typedef uint32_t TbMaterialPerm;
typedef uint32_t TbDrawContextId;
typedef struct cgltf_mesh cgltf_mesh;
typedef struct VkBuffer_T *VkBuffer;
Expand All @@ -31,21 +31,23 @@ static const TbMeshId TbInvalidMeshId = SDL_MAX_UINT64;
static const uint32_t TB_MESH_CMD_PAGE_SIZE = 64;

typedef struct TbPrimitiveDraw {
VkBuffer geom_buffer;
VkIndexType index_type;
uint32_t index_count;
uint32_t material_index;
uint64_t index_offset;
uint32_t vertex_offset;
uint32_t instance_count;
} TbPrimitiveDraw;

typedef struct TbPrimitiveBatch {
uint64_t perm;
VkDescriptorSet view_set;
VkDescriptorSet inst_set;
VkDescriptorSet view_set;
VkDescriptorSet trans_set;
VkDescriptorSet mat_set;
VkDescriptorSet mesh_set;
VkBuffer geom_buffer;
VkDescriptorSet mat_set;
VkDescriptorSet tex_set;
} TbPrimitiveBatch;

typedef TB_DYN_ARR_OF(int32_t) TbIndirectionList;
Expand Down Expand Up @@ -73,7 +75,6 @@ typedef struct TbMeshSystem {

VkDescriptorSetLayout mesh_set_layout;
VkDescriptorSetLayout obj_set_layout;
VkDescriptorSetLayout view_set_layout;
VkPipelineLayout pipe_layout;

VkPipelineLayout prepass_layout;
Expand Down
8 changes: 5 additions & 3 deletions include/rendersystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ typedef struct TbFrameDescriptorPool {
} TbFrameDescriptorPool;

typedef struct TbDescriptorPool {
uint32_t set_count;
VkDescriptorPool set_pool;
uint64_t count;
VkDescriptorPool pool;
uint64_t capacity;
VkDescriptorSet *sets;
} TbDescriptorPool;

Expand Down Expand Up @@ -185,7 +186,8 @@ VkDescriptorSet tb_rnd_frame_desc_pool_get_set(TbRenderSystem *self,
VkResult tb_rnd_resize_desc_pool(TbRenderSystem *self,
const VkDescriptorPoolCreateInfo *pool_info,
const VkDescriptorSetLayout *layouts,
TbDescriptorPool *pool, uint32_t set_count);
void *alloc_next, TbDescriptorPool *pool,
uint32_t set_count);
VkDescriptorSet tb_rnd_desc_pool_get_set(TbDescriptorPool *pool,
uint32_t set_idx);

Expand Down
8 changes: 8 additions & 0 deletions include/texturesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include "SDL2/SDL_stdinc.h"
#include "allocator.h"
#include "dynarray.h"
#include "rendersystem.h"
#include "tbcommon.h"
#include "tbrendercommon.h"

typedef struct TbWorld TbWorld;
typedef struct TbRenderSystem TbRenderSystem;
typedef struct TbTexture TbTexture;
typedef struct VkImageView_T *VkImageView;
typedef struct VkDescriptorSetLayout_T *VkDescriptorSetLayout;
typedef struct cgltf_texture cgltf_texture;
typedef uint64_t TbTextureId;

Expand All @@ -31,6 +33,9 @@ typedef struct TbTextureSystem {

TB_DYN_ARR_OF(TbTexture) textures;

VkDescriptorSetLayout set_layout;
TbDescriptorPool set_pool;

TbTextureId default_color_tex;
TbTextureId default_normal_tex;
TbTextureId default_metal_rough_tex;
Expand All @@ -40,6 +45,9 @@ typedef struct TbTextureSystem {
void tb_register_texture_sys(TbWorld *world);
void tb_unregister_texture_sys(TbWorld *world);

VkDescriptorSet tb_tex_sys_get_set(TbTextureSystem *self);

uint32_t tb_tex_system_get_index(TbTextureSystem *self, TbTextureId tex);
VkImageView tb_tex_system_get_image_view(TbTextureSystem *self,
TbTextureId tex);
TbTextureId tb_tex_system_create_texture(TbTextureSystem *self,
Expand Down
66 changes: 44 additions & 22 deletions source/gltf.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
#include "gltf.hlsli"
#include "lighting.hlsli"

GLTF_MATERIAL_SET(space0)
GLTF_INDIRECT_SET(space1);
GLTF_VIEW_SET(space2);
GLTF_OBJECT_SET(space3);
GLTF_MESH_SET(space4);
GLTF_INDIRECT_SET(space0);
GLTF_VIEW_SET(space1);
GLTF_OBJECT_SET(space2);
GLTF_MESH_SET(space3);
GLTF_TEXTURE_SET(space4);
GLTF_MATERIAL_SET(space5);

[[vk::push_constant]]
ConstantBuffer<GLTFPushConstants> consts : register(b0, space6);

struct VertexIn {
int vert_idx : SV_VertexID;
Expand All @@ -21,11 +25,16 @@ struct Interpolators {
float3 tangent : TANGENT0;
float3 binormal : BINORMAL0;
float2 uv : TEXCOORD0;
int32_t obj_idx : TEXCOORD1;
};

Interpolators vert(VertexIn i) {
int32_t obj_idx = trans_indices[i.inst];
int32_t vert_perm = object_data[obj_idx].perm;
TbCommonObjectData obj_data = object_data[NonUniformResourceIndex(obj_idx)];
int32_t vert_perm = obj_data.perm;
int32_t mat_idx = consts.mat_idx;
GLTFMaterialData gltf = gltf_data[NonUniformResourceIndex(mat_idx)][0];
int32_t mat_perm = gltf.perm;

// Gather vertex data from supplied descriptors
// These functions will give us suitable defaults if this object
Expand All @@ -35,7 +44,8 @@ Interpolators vert(VertexIn i) {
float4 tangent = tb_vert_get_tangent(vert_perm, i.vert_idx, tan_buffer);
int2 uv0 = tb_vert_get_uv0(vert_perm, i.vert_idx, uv0_buffer);

float4x4 m = object_data[obj_idx].m; // Per object model matrix
float4x4 m = object_data[NonUniformResourceIndex(obj_idx)]
.m; // Per object model matrix
float3 world_pos = mul(m, float4(local_pos, 1)).xyz;
float4 clip_pos = mul(camera_data.vp, float4(world_pos, 1.0));

Expand All @@ -48,22 +58,31 @@ Interpolators vert(VertexIn i) {
o.normal = normalize(mul(orientation, normal)); // convert to world-space
o.tangent = normalize(mul(orientation, tangent.xyz));
o.binormal = normalize(cross(o.tangent, o.normal) * tangent.w);
o.uv = uv_transform(uv0, material_data.tex_transform);
o.uv = uv_transform(uv0, gltf.tex_transform);
o.obj_idx = obj_idx;
return o;
}

float4 frag(Interpolators i, bool front_face: SV_IsFrontFace) : SV_TARGET {
TbCommonObjectData obj_data = object_data[NonUniformResourceIndex(i.obj_idx)];
int32_t mat_idx = consts.mat_idx;
GLTFMaterialData gltf = gltf_data[NonUniformResourceIndex(mat_idx)][0];
int32_t mat_perm = gltf.perm;

float4 base_color = 1;

// World-space normal
float3 N = normalize(i.normal);
if (consts.perm & GLTF_PERM_NORMAL_MAP) {
if ((mat_perm & GLTF_PERM_NORMAL_MAP) > 0) {
// Construct TBN
float3x3 tbn = float3x3(normalize(i.binormal), normalize(i.tangent),
normalize(i.normal));

// Convert from tangent space to world space
float3 tangent_space_normal = normal_map.Sample(material_sampler, i.uv).xyz;
float3 tangent_space_normal =
gltf_textures[NonUniformResourceIndex(gltf.normal_idx)]
.Sample(material_sampler, i.uv)
.xyz;
tangent_space_normal = tangent_space_normal * 2 - 1; // Must unpack normal
N = normalize(mul(tangent_space_normal, tbn));
}
Expand All @@ -74,29 +93,32 @@ float4 frag(Interpolators i, bool front_face: SV_IsFrontFace) : SV_TARGET {
float2 screen_uv = (i.clip_pos.xy / i.clip_pos.w) * 0.5 + 0.5;

float4 out_color = 0;
if (consts.perm & GLTF_PERM_PBR_METALLIC_ROUGHNESS) {
float metallic =
material_data.pbr_metallic_roughness.metal_rough_factors[0];
float roughness =
material_data.pbr_metallic_roughness.metal_rough_factors[1];
if ((mat_perm & GLTF_PERM_PBR_METALLIC_ROUGHNESS) > 0) {
PBRMetallicRoughness mr = gltf.pbr_metallic_roughness;
float metallic = mr.metal_rough_factors[0];
float roughness = mr.metal_rough_factors[1];

// TODO: Handle alpha masking
{
base_color = material_data.pbr_metallic_roughness.base_color_factor;
if (consts.perm & GLTF_PERM_BASE_COLOR_MAP) {
base_color *= base_color_map.Sample(material_sampler, i.uv);
base_color = mr.base_color_factor;
if ((mat_perm & GLTF_PERM_BASE_COLOR_MAP) > 0) {
base_color *=
gltf_textures[NonUniformResourceIndex(gltf.color_idx)].Sample(
material_sampler, i.uv);
}
if (consts.perm & GLTF_PERM_ALPHA_CLIP) {
if (base_color.a < ALPHA_CUTOFF(material_data)) {
if (mat_perm & GLTF_PERM_ALPHA_CLIP) {
if (base_color.a < ALPHA_CUTOFF(gltf)) {
discard;
}
}
}

if (consts.perm & GLTF_PERM_PBR_METAL_ROUGH_TEX) {
if ((mat_perm & GLTF_PERM_PBR_METAL_ROUGH_TEX) > 0) {
// The red channel of this texture *may* store occlusion.
// TODO: Check the perm for occlusion
float4 mr_sample = metal_rough_map.Sample(material_sampler, i.uv);
float4 mr_sample =
gltf_textures[NonUniformResourceIndex(gltf.pbr_idx)].Sample(
material_sampler, i.uv);
roughness *= mr_sample.g;
metallic *= mr_sample.b;
}
Expand Down
Loading

0 comments on commit 9f53fc2

Please sign in to comment.