Skip to content

Commit

Permalink
[WIP] meatpipe: export arrays and meatpipe bindings directly
Browse files Browse the repository at this point in the history
compiles and runs, but doesn't draw anything yet
  • Loading branch information
w23 committed Jan 16, 2023
1 parent 8d484d9 commit d96917c
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 209 deletions.
13 changes: 4 additions & 9 deletions ref_vk/ray_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "vk_descriptor.h"

// FIXME this is only needed for MAX_CONCURRENT_FRAMES
// TODO specify it externally as ctor arg
#include "vk_framectl.h"

#define MAX_STAGES 16
Expand All @@ -22,7 +23,6 @@ typedef struct ray_pass_s {
struct {
vk_descriptors_t riptors;
VkDescriptorSet sets[MAX_CONCURRENT_FRAMES];
int *binding_semantics;
} desc;
} ray_pass_t;

Expand All @@ -49,10 +49,6 @@ static void initPassDescriptors( ray_pass_t *header, const ray_pass_layout_t *la
}

static void finalizePassDescriptors( ray_pass_t *header, const ray_pass_layout_t *layout ) {
const size_t semantics_size = sizeof(int) * layout->bindings_count;
header->desc.binding_semantics = Mem_Malloc(vk_core.pool, semantics_size);
memcpy(header->desc.binding_semantics, layout->bindings_semantics, semantics_size);

const size_t bindings_size = sizeof(layout->bindings[0]) * layout->bindings_count;
VkDescriptorSetLayoutBinding *bindings = Mem_Malloc(vk_core.pool, bindings_size);
memcpy(bindings, layout->bindings, bindings_size);
Expand Down Expand Up @@ -228,7 +224,6 @@ void RayPassDestroy( struct ray_pass_s *pass ) {

VK_DescriptorsDestroy(&pass->desc.riptors);
Mem_Free(pass->desc.riptors.values);
Mem_Free(pass->desc.binding_semantics);
Mem_Free((void*)pass->desc.riptors.bindings);
Mem_Free(pass);
}
Expand All @@ -248,12 +243,11 @@ static void performCompute( VkCommandBuffer cmdbuf, int set_slot, const ray_pass
vkCmdDispatch(cmdbuf, (res->width + WG_W - 1) / WG_W, (res->height + WG_H - 1) / WG_H, 1);
}

void RayPassPerform( VkCommandBuffer cmdbuf, int frame_set_slot, struct ray_pass_s *pass, struct vk_ray_resources_s *res) {
/*
void RayPassPerform( VkCommandBuffer cmdbuf, int frame_set_slot, struct ray_pass_s *pass, const struct vk_ray_resources_s *res) {
{
ray_resources_fill_t fill = {
.resources = res,
.count = pass->desc.riptors.num_bindings,
.indices = pass->desc.binding_semantics,
.out_values = pass->desc.riptors.values,
};
Expand Down Expand Up @@ -292,3 +286,4 @@ void RayPassPerform( VkCommandBuffer cmdbuf, int frame_set_slot, struct ray_pass
DEBUG_END(cmdbuf);
}
*/
14 changes: 11 additions & 3 deletions ref_vk/ray_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// - expose it as a struct[] interface of the pass
// - resource/interface should prepare descriptors outside of pass code and just pass them to pass
typedef struct {
const int *bindings_semantics; // RayResource_something
const VkDescriptorSetLayoutBinding *bindings;
int bindings_count;

Expand All @@ -19,6 +18,7 @@ struct ray_pass_s;
typedef struct ray_pass_s* ray_pass_p;

typedef struct {
// TODO int num_frame_slots;
const char *debug_name;
ray_pass_layout_t layout;

Expand All @@ -35,6 +35,7 @@ typedef struct {
} ray_pass_hit_group_t;

typedef struct {
// TODO int num_frame_slots;
const char *debug_name;
ray_pass_layout_t layout;

Expand All @@ -57,5 +58,12 @@ struct ray_pass_s *RayPassCreateTracing( const ray_pass_create_tracing_t *create

void RayPassDestroy( struct ray_pass_s *pass );

struct vk_ray_resources_s;
void RayPassPerform( VkCommandBuffer cmdbuf, int frame_set_slot, struct ray_pass_s *pass, struct vk_ray_resources_s *res);
struct vk_ray_resource_handle_s;
typedef struct ray_pass_perform_args_s {
int frame_set_slot; // 0 or 1, until we do num_frame_slots
int width, height;
const struct vk_ray_resource_handle_s *resources;
} ray_pass_perform_args_t;

void RayPassPerform(struct ray_pass_s *pass, VkCommandBuffer cmdbuf, ray_pass_perform_args_t args );

101 changes: 83 additions & 18 deletions ref_vk/ray_resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,67 @@
#include <stdlib.h>

#define MAX_BARRIERS 16
#define MAX_RESOURCES 32
#define MAX_NAME 32

typedef struct vk_named_resource_t {
char name[MAX_NAME];
int count;
ray_resource_desc_t desc_fixme;
} vk_named_resource_t;

static struct {
vk_named_resource_t named[MAX_RESOURCES];
ray_resource_t resources[RayResource__COUNT];
} g_resources;

static int findSlot(const char* name) {
// Find the exact match if exists
// There might be gaps, so we need to check everything
for (int i = 0; i < MAX_RESOURCES; ++i) {
if (strcmp(g_resources.named[i].name, name) == 0)
return i;
}

// Find first free slot
for (int i = 0; i < MAX_RESOURCES; ++i) {
if (!g_resources.named[i].name[0])
return i;
}

return -1;
}

qboolean R_VkResourceSetExternal(const char* name, VkDescriptorType type, vk_descriptor_value_t value, int count, ray_resource_desc_t desc, const xvk_image_t *image) {
if (strlen(name) >= MAX_NAME)
return false;

const int index = findSlot(name);
if (index < 0)
return false;

vk_named_resource_t *const r = g_resources.named + index;
ray_resource_t *const rr = g_resources.resources + index;

if (!r->name[0]) {
strncpy(r->name, name, sizeof(r->name));
rr->type = type;
r->count = count;
r->desc_fixme = desc;
} else {
ASSERT(rr->type == type);
ASSERT(r->count == count);
ASSERT(r->desc_fixme.type == desc.type);
ASSERT(r->desc_fixme.image_format == desc.image_format);
}

rr->value = value;
rr->image = image;
memset(&rr->read, 0, sizeof(rr->read));
memset(&rr->write, 0, sizeof(rr->write));

return true;
}

void RayResourcesFill(VkCommandBuffer cmdbuf, ray_resources_fill_t fill) {
VkImageMemoryBarrier image_barriers[MAX_BARRIERS];
Expand All @@ -15,7 +76,7 @@ void RayResourcesFill(VkCommandBuffer cmdbuf, ray_resources_fill_t fill) {
for (int i = 0; i < fill.count; ++i) {
const qboolean write = fill.indices[i] < 0;
const int index = abs(fill.indices[i]) - 1;
ray_resource_t *const res = fill.resources->resources + index;
ray_resource_t *const res = g_resources.resources + index;

ASSERT(index >= 0);
ASSERT(index < RayResource__COUNT);
Expand Down Expand Up @@ -97,15 +158,14 @@ void RayResourcesFill(VkCommandBuffer cmdbuf, ray_resources_fill_t fill) {
0, 0, NULL, 0, NULL, image_barriers_count, image_barriers);
}
}

#if 0
static const struct {
const char *name;
ray_resource_type_e type;
int image_format;
ray_resource_binding_desc_fixme_t desc;
} fixme_descs[] = {

#define FIXME_DESC_BUFFER(name_, semantic_, count_) \
{ .name = name_, \
{ \
.type = ResourceBuffer, \
.desc.semantic = (RayResource_##semantic_ + 1), \
.desc.count = count_, \
Expand All @@ -122,7 +182,7 @@ static const struct {
FIXME_DESC_BUFFER("light_grid", light_clusters, 1),

#define FIXME_DESC_IMAGE(name_, semantic_, image_format_, count_) \
{ .name = name_, \
{ \
.type = ResourceImage, \
.image_format = image_format_, \
.desc.semantic = (RayResource_##semantic_ + 1), \
Expand All @@ -135,7 +195,7 @@ static const struct {

// Internal, temporary
#define DECLARE_IMAGE(_, name_, format_) \
{ .name = #name_, \
{ \
.type = ResourceImage, \
.image_format = format_, \
.desc.semantic = (RayResource_##name_ + 1), \
Expand All @@ -148,23 +208,28 @@ static const struct {
RAY_LIGHT_DIRECT_POLY_OUTPUTS(DECLARE_IMAGE)
RAY_LIGHT_DIRECT_POINT_OUTPUTS(DECLARE_IMAGE)
};
#endif

const ray_resource_binding_desc_fixme_t *RayResouceGetBindingForName_FIXME(const char *name, ray_resource_desc_t desc) {
for (int i = 0; i < COUNTOF(fixme_descs); ++i) {
if (strcmp(name, fixme_descs[i].name) != 0)
/*
ray_resource_binding_desc_fixme_t RayResouceGetBindingForName_FIXME(const char *name, ray_resource_desc_t desc) {
for (int i = 0; i < MAX_RESOURCES; ++i) {
const vk_named_resource_t *const res = g_resources.named + i;
if (strcmp(name, res->name) != 0)
continue;
if (fixme_descs[i].type != desc.type) {
gEngine.Con_Printf(S_ERROR "Incompatible resource types for name %s: want %d, have %d\n", name, desc.type, fixme_descs[i].type);
return NULL;
if (res->desc_fixme.type != desc.type) {
gEngine.Con_Printf(S_ERROR "Incompatible resource types for name %s: want %d, have %d\n", name, desc.type, res->desc_fixme.type);
break;
}
if (fixme_descs[i].type == ResourceImage && fixme_descs[i].image_format != VK_FORMAT_UNDEFINED && desc.image_format != fixme_descs[i].image_format) {
gEngine.Con_Printf(S_ERROR "Incompatible image formats for name %s: want %d, have %d\n", name, desc.image_format, fixme_descs[i].image_format);
return NULL;
if (res->desc_fixme.type == ResourceImage && res->desc_fixme.image_format != VK_FORMAT_UNDEFINED && desc.image_format != res->desc_fixme.image_format) {
gEngine.Con_Printf(S_ERROR "Incompatible image formats for name %s: want %d, have %d\n", name, desc.image_format, res->desc_fixme.image_format);
break;
}
return &fixme_descs[i].desc;
return (ray_resource_binding_desc_fixme_t){i, res->count};
}
return NULL;
return (ray_resource_binding_desc_fixme_t){-1,-1};
}
*/
54 changes: 29 additions & 25 deletions ref_vk/ray_resources.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
#pragma once

#include "vk_rtx.h"
#include "vk_const.h"
#include "vk_image.h"
#include "vk_descriptor.h"

#include "shaders/ray_interop.h"

//qboolean R_VkResourceInit(void);
//void R_VkResourceShutdown();

typedef enum {
ResourceUnknown,
ResourceBuffer,
ResourceImage,
} ray_resource_type_e;

typedef struct {
ray_resource_type_e type;
int image_format; // if type == ResourceImage
} ray_resource_desc_t;

qboolean R_VkResourceSetExternal(const char* name, VkDescriptorType type, vk_descriptor_value_t value, int count, ray_resource_desc_t desc, const xvk_image_t *image);

/*
typedef struct {
int semantic;
int count;
} ray_resource_binding_desc_fixme_t;
ray_resource_binding_desc_fixme_t RayResouceGetBindingForName_FIXME(const char *name, ray_resource_desc_t desc);
*/

#define RAY_SCENE_RESOURCES(X) \
X(TLAS, tlas) \
X(Buffer, ubo) \
Expand All @@ -29,6 +53,10 @@ enum {
RayResource__COUNT
};

typedef struct vk_ray_resources_s {
uint32_t width, height;
} vk_ray_resources_t;

typedef struct {
VkAccessFlags access_mask;
VkImageLayout image_layout;
Expand All @@ -44,13 +72,7 @@ typedef struct {
};
} ray_resource_t;

typedef struct vk_ray_resources_s {
uint32_t width, height;
ray_resource_t resources[RayResource__COUNT];
} vk_ray_resources_t;

typedef struct {
vk_ray_resources_t *resources;
const int *indices;
int count;
VkPipelineStageFlagBits dest_pipeline;
Expand All @@ -59,21 +81,3 @@ typedef struct {
} ray_resources_fill_t;

void RayResourcesFill(VkCommandBuffer cmdbuf, ray_resources_fill_t fill);

typedef struct {
int semantic;
int count;
} ray_resource_binding_desc_fixme_t;

typedef enum {
ResourceUnknown,
ResourceBuffer,
ResourceImage,
} ray_resource_type_e;

typedef struct {
ray_resource_type_e type;
int image_format; // if type == ResourceImage
} ray_resource_desc_t;

const ray_resource_binding_desc_fixme_t *RayResouceGetBindingForName_FIXME(const char *name, ray_resource_desc_t desc);
Loading

0 comments on commit d96917c

Please sign in to comment.