Skip to content

Commit

Permalink
Making sure resources are packed properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Honeybunch committed Sep 1, 2024
1 parent d3f3d1f commit 58a9061
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 26 deletions.
10 changes: 5 additions & 5 deletions addons/water/include/tb_ocean.slangh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// To avoid struct packing issues
typedef float4 TbOceanWave; // xy = dir, z = steep, w = wavelength

TB_GPU_STRUCT_DECL(TbOceanData, { float4 time_waves; // x = time, y = wave count
TbOceanWave wave[TB_WAVE_MAX];});


TB_GPU_STRUCT_DECL(TbOceanPushConstants, {
TB_GPU_STRUCT_DECL_NOPACK(TbOceanData, {
float4 time_waves; // x = time, y = wave count
TbOceanWave wave[TB_WAVE_MAX];
});
TB_GPU_STRUCT_DECL(TbOceanPushConstants, {
float4x4 m;
});

Expand Down
11 changes: 6 additions & 5 deletions include/tb_common.slangh
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@

#ifdef TB_SHADER
#define _TB_GPU_STRUCT_START struct
#else
#define _TB_GPU_STRUCT_START typedef struct TB_GPU_STRUCT
#endif

#ifdef TB_SHADER
#define _TB_GPU_STRUCT_START_NOPACK struct
#define _TB_GPU_STRUCT_END(name)
#else
#define _TB_GPU_STRUCT_START typedef struct TB_GPU_STRUCT
#define _TB_GPU_STRUCT_START_NOPACK typedef struct
#define _TB_GPU_STRUCT_END(name) name
#endif

#define TB_GPU_STRUCT_DECL(name, body) \
_TB_GPU_STRUCT_START name body _TB_GPU_STRUCT_END(name)

#define TB_GPU_STRUCT_DECL_NOPACK(name, body) \
_TB_GPU_STRUCT_START_NOPACK name body _TB_GPU_STRUCT_END(name)

TB_GPU_STRUCT_DECL(TbCommonObjectData, {float4x4 m;});

// Common input layout info and permutation settings
Expand Down
2 changes: 1 addition & 1 deletion include/tb_gltf.slangh
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ float2 uv_transform(int2 quant_uv, TextureTransform trans) {
[[vk::binding(1, b)]] TextureCube irradiance_map; \
[[vk::binding(2, b)]] TextureCube prefiltered_map; \
[[vk::binding(3, b)]] Texture2D brdf_lut; \
[[vk::binding(4, b)]] ConstantBuffer<TbCommonLightData> light_data; \
[[vk::binding(4, b)]] ConstantBuffer<TbLightData> light_data; \
[[vk::binding(5, b)]] Texture2DArray shadow_map; \
[[vk::binding(6, b)]] SamplerState filtered_env_sampler; \
[[vk::binding(7, b)]] SamplerState brdf_sampler;
Expand Down
4 changes: 2 additions & 2 deletions include/tb_lighting.slangh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "tb_pbr.slangh"

TB_GPU_STRUCT_DECL(TbCommonLightData,{
TB_GPU_STRUCT_DECL_NOPACK(TbLightData,{
float3 color;
float3 light_dir;
float4 cascade_splits;
Expand All @@ -20,7 +20,7 @@ struct TbView {
};

struct Light {
TbCommonLightData light;
TbLightData light;
Texture2DArray shadow_map;
SamplerComparisonState shadow_sampler;
};
Expand Down
5 changes: 4 additions & 1 deletion include/tb_sky.slangh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ _Static_assert(sizeof(TbSkyPushConstants) <= TB_PUSH_CONSTANT_BYTES,
"Too Many Push Constants");
#endif

TB_GPU_STRUCT_DECL(TbSkyData, {
// HACK
// If we pack this it won't work with the ocean
// Unintuitive and shitty
TB_GPU_STRUCT_DECL_NOPACK(TbSkyData, {
float time;
float cirrus;
float cumulus;
Expand Down
2 changes: 1 addition & 1 deletion include/tb_view.slangh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "tb_common.slangh"

TB_GPU_STRUCT_DECL(TbViewData, {
TB_GPU_STRUCT_DECL_NOPACK(TbViewData, {
float4x4 v;
float4x4 p;
float4x4 vp;
Expand Down
4 changes: 2 additions & 2 deletions include/tb_view_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ typedef struct TbViewSystemFrameState {
typedef struct TbView {
TbRenderTargetId target;
TbViewData view_data;
TbCommonLightData light_data;
TbLightData light_data;
TbFrustum frustum;
TbDescriptorBuffer desc_buffer;
} TbView;
Expand Down Expand Up @@ -65,7 +65,7 @@ void tb_view_system_set_view_target(TbViewSystem *self, TbViewId view,
void tb_view_system_set_view_data(TbViewSystem *self, TbViewId view,
const TbViewData *data);
void tb_view_system_set_light_data(TbViewSystem *self, TbViewId view,
const TbCommonLightData *data);
const TbLightData *data);
void tb_view_system_set_view_frustum(TbViewSystem *self, TbViewId view,
const TbFrustum *frust);
VkDescriptorSet tb_view_system_get_descriptor(TbViewSystem *self,
Expand Down
2 changes: 1 addition & 1 deletion source/tb_light_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void light_update_tick(ecs_iter_t *it) {
float3 dir = -tb_transform_get_forward(&transform->transform);

// Send lighting data to the camera's view
TbCommonLightData light_data = {
TbLightData light_data = {
.color = light->color,
.light_dir = dir,
.cascade_splits = light->cascade_splits,
Expand Down
16 changes: 8 additions & 8 deletions source/tb_view_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void view_update_tick(ecs_iter_t *it) {
TB_DYN_ARR_FOREACH(sys->views, view_idx) {
const TbView *view = &TB_DYN_ARR_AT(sys->views, view_idx);
const TbViewData *view_data = &view->view_data;
const TbCommonLightData *light_data = &view->light_data;
const TbLightData *light_data = &view->light_data;

tb_auto tmp_addr = tb_rnd_get_gpu_tmp_addr(rnd_sys);

Expand All @@ -266,7 +266,7 @@ void view_update_tick(ecs_iter_t *it) {
tb_rnd_sys_copy_to_tmp_buffer(rnd_sys, sizeof(TbViewData), 0x40,
view_data, &view_offset);
uint64_t light_offset = 0;
tb_rnd_sys_copy_to_tmp_buffer(rnd_sys, sizeof(TbCommonLightData), 0x40,
tb_rnd_sys_copy_to_tmp_buffer(rnd_sys, sizeof(TbLightData), 0x40,
light_data, &light_offset);

TB_DYN_ARR_OF(TbDescriptor) descriptors = {0};
Expand Down Expand Up @@ -336,7 +336,7 @@ void view_update_tick(ecs_iter_t *it) {
.pUniformBuffer = &(VkDescriptorAddressInfoEXT){
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_ADDRESS_INFO_EXT,
.address = tmp_addr + light_offset,
.range = sizeof(TbCommonLightData),
.range = sizeof(TbLightData),
}}}));

// Write all descriptors to buffer
Expand Down Expand Up @@ -418,16 +418,16 @@ void view_update_tick(ecs_iter_t *it) {
TB_DYN_ARR_FOREACH(sys->views, view_idx) {
const TbView *view = &TB_DYN_ARR_AT(sys->views, view_idx);
const TbViewData *view_data = &view->view_data;
const TbCommonLightData *light_data = &view->light_data;
const TbLightData *light_data = &view->light_data;

// Write view data into the tmp buffer we know will wind up on the GPU
uint64_t view_offset = 0;
err = tb_rnd_sys_copy_to_tmp_buffer(rnd_sys, sizeof(TbViewData), 0x40,
view_data, &view_offset);
TB_VK_CHECK(err, "Failed to make tmp host buffer allocation for view");
uint64_t light_offset = 0;
err = tb_rnd_sys_copy_to_tmp_buffer(rnd_sys, sizeof(TbCommonLightData),
0x40, light_data, &light_offset);
err = tb_rnd_sys_copy_to_tmp_buffer(rnd_sys, sizeof(TbLightData), 0x40,
light_data, &light_offset);
TB_VK_CHECK(err, "Failed to make tmp host buffer allocation for view");

uint32_t buffer_idx = view_idx * buf_count;
Expand All @@ -445,7 +445,7 @@ void view_update_tick(ecs_iter_t *it) {
buffer_info[buffer_idx + 1] = (VkDescriptorBufferInfo){
.buffer = tmp_gpu_buffer,
.offset = light_offset,
.range = sizeof(TbCommonLightData),
.range = sizeof(TbLightData),
};

image_info[image_idx + 0] = (VkDescriptorImageInfo){
Expand Down Expand Up @@ -599,7 +599,7 @@ void tb_view_system_set_view_data(TbViewSystem *self, TbViewId view,
}

void tb_view_system_set_light_data(TbViewSystem *self, TbViewId view,
const TbCommonLightData *data) {
const TbLightData *data) {
if (view >= TB_DYN_ARR_SIZE(self->views)) {
TB_CHECK(false, "TbView Id out of range");
}
Expand Down

0 comments on commit 58a9061

Please sign in to comment.