Skip to content

Commit 3faab0d

Browse files
committed
Pass:send supports raytracer; Bugfixes;
1 parent fcaa90f commit 3faab0d

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

src/api/l_graphics.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ static int l_lovrGraphicsGetFeatures(lua_State* L) {
489489
lua_pushboolean(L, features.wireframe), lua_setfield(L, -2, "wireframe");
490490
lua_pushboolean(L, features.depthClamp), lua_setfield(L, -2, "depthClamp");
491491
lua_pushboolean(L, features.depthResolve), lua_setfield(L, -2, "depthResolve");
492+
lua_pushboolean(L, features.raytracing), lua_setfield(L, -2, "raytracing");
492493
lua_pushboolean(L, features.indirectDrawFirstInstance), lua_setfield(L, -2, "indirectDrawFirstInstance");
493494
lua_pushboolean(L, features.packedBuffers), lua_setfield(L, -2, "packedBuffers");
494495
lua_pushboolean(L, features.float64), lua_setfield(L, -2, "float64");

src/api/l_graphics_pass.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static int l_lovrPassSend(lua_State* L) {
727727
const char* name = luaL_checklstring(L, 2, &length);
728728

729729
if (lua_isnoneornil(L, 3)) {
730-
return luax_typeerror(L, 3, "Buffer, Texture, Sampler, number, vector, table, or boolean");
730+
return luax_typeerror(L, 3, "Buffer, Texture, Sampler, Raytracer, number, vector, table, or boolean");
731731
}
732732

733733
Buffer* buffer = luax_totype(L, 3, Buffer);
@@ -753,6 +753,13 @@ static int l_lovrPassSend(lua_State* L) {
753753
return 0;
754754
}
755755

756+
Raytracer* raytracer = luax_totype(L, 3, Raytracer);
757+
758+
if (raytracer) {
759+
luax_assert(L, lovrPassSendRaytracer(pass, name, length, raytracer));
760+
return 0;
761+
}
762+
756763
void* pointer;
757764
DataField* format;
758765
luax_assert(L, lovrPassSendData(pass, name, length, &pointer, &format));

src/core/gpu_vk.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ bool gpu_tree_init(gpu_tree* tree, gpu_tree_info* info) {
505505
.vertexFormat = convertAttributeType(mesh->vertexType),
506506
.vertexStride = mesh->vertexStride,
507507
.maxVertex = mesh->vertexCount,
508-
.indexType = mesh->indexOffset == ~0u ? (VkIndexType) mesh->indexType : VK_INDEX_TYPE_NONE_KHR
508+
.indexType = mesh->indexOffset == ~0u ? VK_INDEX_TYPE_NONE_KHR : (VkIndexType) mesh->indexType
509509
}
510510
};
511511

@@ -3753,7 +3753,7 @@ static VkBufferUsageFlags getBufferUsage(gpu_buffer_type type) {
37533753
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT |
37543754
VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
37553755
VK_BUFFER_USAGE_TRANSFER_DST_BIT |
3756-
(state.extensions.bufferDeviceAddress ? VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT : 0);
3756+
(state.extensions.bufferDeviceAddress ? VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT : 0) |
37573757
(state.extensions.accelerationStructure ? VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR : 0);
37583758
case GPU_BUFFER_STREAM:
37593759
return

src/modules/graphics/graphics.c

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ struct Shader {
150150
uint32_t bufferMask;
151151
uint32_t textureMask;
152152
uint32_t samplerMask;
153+
uint32_t raytracerMask;
153154
uint32_t storageMask;
154155
uint32_t pushConstantSize;
155156
uint32_t uniformSize;
@@ -299,10 +300,11 @@ struct Model {
299300

300301
struct Raytracer {
301302
uint32_t ref;
303+
uint32_t count;
302304
RaytracerInfo info;
305+
Sync sync;
303306
gpu_tree* gpu;
304307
gpu_tree_instance* instances;
305-
uint32_t count;
306308
bool canUpdate;
307309
};
308310

@@ -414,8 +416,8 @@ typedef struct {
414416
void* next;
415417
uint64_t count;
416418
uint64_t textureMask;
417-
uint64_t padding;
418-
Access list[41];
419+
uint64_t raytracerMask;
420+
Access list[41]; // 1024 bytes total
419421
} AccessBlock;
420422

421423
typedef struct {
@@ -621,10 +623,11 @@ static uint32_t measureTexture(TextureFormat format, uint32_t w, uint32_t h, uin
621623
static bool checkTextureBounds(const TextureInfo* info, uint32_t offset[4], uint32_t extent[3]);
622624
static void mipmapTexture(gpu_stream* stream, Texture* texture, uint32_t base, uint32_t count);
623625
static ShaderResource* findShaderResource(Shader* shader, const char* name, size_t length);
624-
static Access* getNextAccess(Pass* pass, int type, bool texture);
626+
static Access* getNextAccess(Pass* pass, int type, bool texture, bool raytracer);
625627
static void trackBuffer(Pass* pass, Buffer* buffer, gpu_phase phase, gpu_cache cache);
626628
static void trackTexture(Pass* pass, Texture* texture, gpu_phase phase, gpu_cache cache);
627629
static void trackMaterial(Pass* pass, Material* material);
630+
static void trackRaytracer(Pass* pass, Raytracer* raytracer, gpu_phase phase, gpu_cache cache);
628631
static bool syncResource(Access* access, gpu_barrier* barrier);
629632
static gpu_barrier syncTransfer(Sync* sync, gpu_phase phase, gpu_cache cache);
630633
static void updateModelTransforms(Model* model, uint32_t nodeIndex, float* parent);
@@ -950,6 +953,7 @@ void lovrGraphicsGetFeatures(GraphicsFeatures* features) {
950953
features->wireframe = state.features.wireframe;
951954
features->depthClamp = state.features.depthClamp;
952955
features->depthResolve = state.features.depthResolve;
956+
features->raytracing = state.features.rayQuery;
953957
features->indirectDrawFirstInstance = state.features.indirectDrawFirstInstance;
954958
features->packedBuffers = state.features.packedBuffers;
955959
features->float64 = state.features.float64;
@@ -3542,11 +3546,13 @@ Shader* lovrShaderCreate(const ShaderInfo* info) {
35423546
bool buffer = resource->type == SPV_UNIFORM_BUFFER || resource->type == SPV_STORAGE_BUFFER;
35433547
bool texture = resource->type == SPV_SAMPLED_TEXTURE || resource->type == SPV_STORAGE_TEXTURE || resource->type == SPV_COMBINED_TEXTURE_SAMPLER;
35443548
bool sampler = resource->type == SPV_SAMPLER;
3549+
bool raytracer = resource->type == SPV_ACCELERATION_STRUCTURE;
35453550
bool storage = resource->type == SPV_STORAGE_BUFFER || resource->type == SPV_STORAGE_TEXTURE;
35463551

35473552
shader->bufferMask |= (buffer << *binding);
35483553
shader->textureMask |= (texture << *binding);
35493554
shader->samplerMask |= (sampler << *binding);
3555+
shader->raytracerMask |= (raytracer << *binding);
35503556
shader->storageMask |= (storage << *binding);
35513557

35523558
gpu_cache cache;
@@ -5967,8 +5973,13 @@ static void lovrPassRelease(Pass* pass) {
59675973
for (uint32_t i = 0; i < COUNTOF(pass->access); i++) {
59685974
for (AccessBlock* block = pass->access[i]; block != NULL; block = block->next) {
59695975
for (uint32_t j = 0; j < block->count; j++) {
5970-
bool texture = block->textureMask & (1ull << j);
5971-
lovrRelease(block->list[j].object, texture ? lovrTextureDestroy : lovrBufferDestroy);
5976+
if (block->textureMask & (1ull << j)) {
5977+
lovrRelease(block->list[j].object, lovrTextureDestroy);
5978+
} else if (block->raytracerMask & (1ull << j)) {
5979+
lovrRelease(block->list[j].object, lovrRaytracerDestroy);
5980+
} else {
5981+
lovrRelease(block->list[j].object, lovrBufferDestroy);
5982+
}
59725983
}
59735984
}
59745985
}
@@ -6979,6 +6990,22 @@ bool lovrPassSendSampler(Pass* pass, const char* name, size_t length, Sampler* s
69796990
return true;
69806991
}
69816992

6993+
bool lovrPassSendRaytracer(Pass* pass, const char* name, size_t length, Raytracer* raytracer) {
6994+
Shader* shader = pass->pipeline->shader;
6995+
lovrCheck(shader, "A Shader must be active to send resources");
6996+
6997+
ShaderResource* resource = findShaderResource(shader, name, length);
6998+
lovrCheck(resource, "Shader has no variable named '%s'", name);
6999+
uint32_t slot = resource->binding;
7000+
7001+
lovrCheck(shader->raytracerMask & (1u << slot), "Trying to send a Raytracer to '%s', but the shader variable isn't a Raytracer", name);
7002+
7003+
trackRaytracer(pass, raytracer, resource->phase, resource->cache);
7004+
pass->bindings[slot].tree = raytracer->gpu;
7005+
pass->flags |= DIRTY_BINDINGS;
7006+
return true;
7007+
}
7008+
69827009
bool lovrPassSendData(Pass* pass, const char* name, size_t length, void** data, DataField** format) {
69837010
Shader* shader = pass->pipeline->shader;
69847011
lovrCheck(shader, "A Shader must be active to send data to it");
@@ -8976,7 +9003,7 @@ static ShaderResource* findShaderResource(Shader* shader, const char* name, size
89769003
return NULL;
89779004
}
89789005

8979-
static Access* getNextAccess(Pass* pass, int type, bool texture) {
9006+
static Access* getNextAccess(Pass* pass, int type, bool texture, bool raytracer) {
89809007
AccessBlock* block = pass->access[type];
89819008

89829009
if (!block || block->count >= COUNTOF(block->list)) {
@@ -8985,16 +9012,18 @@ static Access* getNextAccess(Pass* pass, int type, bool texture) {
89859012
new->next = block;
89869013
new->count = 0;
89879014
new->textureMask = 0;
9015+
new->raytracerMask = 0;
89889016
block = new;
89899017
}
89909018

89919019
block->textureMask |= (uint64_t) texture << block->count;
9020+
block->raytracerMask |= (uint64_t) raytracer << block->count;
89929021
return &block->list[block->count++];
89939022
}
89949023

89959024
static void trackBuffer(Pass* pass, Buffer* buffer, gpu_phase phase, gpu_cache cache) {
89969025
if (!buffer) return;
8997-
Access* access = getNextAccess(pass, phase == GPU_PHASE_SHADER_COMPUTE ? ACCESS_COMPUTE : ACCESS_RENDER, false);
9026+
Access* access = getNextAccess(pass, phase == GPU_PHASE_SHADER_COMPUTE ? ACCESS_COMPUTE : ACCESS_RENDER, false, false);
89989027
access->sync = buffer->sync;
89999028
access->object = buffer;
90009029
access->phase = phase;
@@ -9011,7 +9040,7 @@ static void trackTexture(Pass* pass, Texture* texture, gpu_phase phase, gpu_cach
90119040
cache = 0;
90129041
}
90139042

9014-
Access* access = getNextAccess(pass, phase == GPU_PHASE_SHADER_COMPUTE ? ACCESS_COMPUTE : ACCESS_RENDER, true);
9043+
Access* access = getNextAccess(pass, phase == GPU_PHASE_SHADER_COMPUTE ? ACCESS_COMPUTE : ACCESS_RENDER, true, false);
90159044
access->sync = texture->sync;
90169045
access->object = texture;
90179046
access->phase = phase;
@@ -9036,6 +9065,15 @@ static void trackMaterial(Pass* pass, Material* material) {
90369065
trackTexture(pass, material->info.normalTexture, phase, cache);
90379066
}
90389067

9068+
static void trackRaytracer(Pass* pass, Raytracer* raytracer, gpu_phase phase, gpu_cache cache) {
9069+
Access* access = getNextAccess(pass, phase == GPU_PHASE_SHADER_COMPUTE ? ACCESS_COMPUTE : ACCESS_RENDER, false, true);
9070+
access->sync = &raytracer->sync;
9071+
access->object = raytracer;
9072+
access->phase = phase;
9073+
access->cache = cache;
9074+
lovrRetain(raytracer);
9075+
}
9076+
90399077
static bool syncResource(Access* access, gpu_barrier* barrier) {
90409078
// There are 4 types of access patterns:
90419079
// - read after read:

src/modules/graphics/graphics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct {
4747
bool wireframe;
4848
bool depthClamp;
4949
bool depthResolve;
50+
bool raytracing;
5051
bool indirectDrawFirstInstance;
5152
bool packedBuffers;
5253
bool float64;
@@ -701,6 +702,7 @@ void lovrPassSetWireframe(Pass* pass, bool wireframe);
701702
bool lovrPassSendBuffer(Pass* pass, const char* name, size_t length, Buffer* buffer, uint32_t offset, uint32_t extent);
702703
bool lovrPassSendTexture(Pass* pass, const char* name, size_t length, Texture* texture);
703704
bool lovrPassSendSampler(Pass* pass, const char* name, size_t length, Sampler* sampler);
705+
bool lovrPassSendRaytracer(Pass* pass, const char* name, size_t length, Raytracer* raytracer);
704706
bool lovrPassSendData(Pass* pass, const char* name, size_t length, void** data, DataField** format);
705707

706708
bool lovrPassPoints(Pass* pass, uint32_t count, float** vertices);

0 commit comments

Comments
 (0)