Skip to content

Commit d9566d2

Browse files
committed
Support for more Ray Tracing SPIR-V
1 parent d4407ab commit d9566d2

File tree

10 files changed

+71
-2
lines changed

10 files changed

+71
-2
lines changed

include/shady/grammar.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@
7373
"name": "Image",
7474
"llvm-id": 397
7575
},
76+
{
77+
"name": "IncomingRayPayload",
78+
"llvm-id": 399
79+
},
80+
{
81+
"name": "RayPayload",
82+
"llvm-id": 400
83+
},
84+
{
85+
"name": "HitAttribute",
86+
"llvm-id": 401
87+
},
7688
{
7789
"name": "CallableDataKHR"
7890
},
@@ -783,4 +795,4 @@
783795
]
784796
}
785797
]
786-
}
798+
}

include/shady/ir/execution_model.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ EM(Fragment ) \
99
EM(Vertex ) \
1010
EM(RayGeneration) \
1111
EM(Callable ) \
12+
EM(RayMiss ) \
13+
EM(RayCHit ) \
1214

1315
typedef enum {
1416
ShdExecutionModelNone,
@@ -25,6 +27,8 @@ bool shd_get_workgroup_size_for_entry_point(const Node* decl, uint32_t* out);
2527
static inline bool shd_is_rt_execution_model(ShdExecutionModel em) {
2628
switch (em) {
2729
case ShdExecutionModelRayGeneration:
30+
case ShdExecutionModelRayMiss:
31+
case ShdExecutionModelRayCHit:
2832
case ShdExecutionModelCallable: return true;
2933
default: return false;
3034
}

src/backend/spirv/emit_spv.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "emit_spv.h"
22

33
#include "shady/ir/builtin.h"
4+
#include "shady/ir/execution_model.h"
45
#include "shady/pass.h"
56

67
#include "../shady/ir_private.h"
@@ -257,6 +258,14 @@ void shd_spv_emit_debuginfo(Emitter* emitter, const Node* n, SpvId id) {
257258

258259
static SpvExecutionModel emit_exec_model(Emitter* emitter, ShdExecutionModel model) {
259260
switch (model) {
261+
case ShdExecutionModelRayCHit:
262+
spvb_extension(emitter->file_builder, "SPV_KHR_ray_tracing");
263+
spvb_capability(emitter->file_builder, SpvCapabilityRayTracingKHR);
264+
return SpvExecutionModelClosestHitKHR;
265+
case ShdExecutionModelRayMiss:
266+
spvb_extension(emitter->file_builder, "SPV_KHR_ray_tracing");
267+
spvb_capability(emitter->file_builder, SpvCapabilityRayTracingKHR);
268+
return SpvExecutionModelMissKHR;
260269
case ShdExecutionModelRayGeneration:
261270
spvb_extension(emitter->file_builder, "SPV_KHR_ray_tracing");
262271
spvb_capability(emitter->file_builder, SpvCapabilityRayTracingKHR);

src/backend/spirv/emit_spv_type.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "emit_spv.h"
22

3+
#include "shady/ir/enum.h"
34
#include "shady/ir/memory_layout.h"
45

56
#include "shady/rewrite.h"
67

78
#include "portability.h"
89
#include "log.h"
910
#include "dict.h"
11+
#include "spirv/unified1/spirv.h"
1012

1113
#include <assert.h>
1214

@@ -32,6 +34,9 @@ SpvStorageClass spv_emit_addr_space(Emitter* emitter, AddressSpace address_space
3234
case AsUniformConstant: return SpvStorageClassUniformConstant;
3335
case AsIncomingCallableDataKHR: return SpvStorageClassIncomingCallableDataKHR;
3436
case AsCallableDataKHR: return SpvStorageClassCallableDataKHR;
37+
case AsIncomingRayPayload: return SpvStorageClassIncomingRayPayloadKHR;
38+
case AsRayPayload: return SpvStorageClassRayPayloadKHR;
39+
case AsHitAttribute: return SpvStorageClassHitAttributeKHR;
3540

3641
case AsCode: return SpvStorageClassCodeSectionSHADY;
3742

src/frontend/llvm/l2s_annotations.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ static bool is_io_as(AddressSpace as) {
5454
case AsUInput:
5555
case AsOutput:
5656
case AsUniform:
57+
case AsIncomingRayPayload:
58+
case AsRayPayload:
59+
case AsHitAttribute:
5760
case AsUniformConstant: return true;
5861
default: break;
5962
}

src/frontend/spirv/s2s.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,8 @@ static size_t parse_spv_instruction_at(SpvParser* parser, size_t instruction_off
892892
shd_int32_literal(parser->arena, wg_size_dec->payload.literals.data[1]),
893893
shd_int32_literal(parser->arena, wg_size_dec->payload.literals.data[2]))
894894
}));
895+
} else if (strcmp(entry_point_type->payload.str, "RayCHit") == 0) {
896+
} else if (strcmp(entry_point_type->payload.str, "RayMiss") == 0) {
895897
} else if (strcmp(entry_point_type->payload.str, "Fragment") == 0) {
896898

897899
} else if (strcmp(entry_point_type->payload.str, "Vertex") == 0) {

src/shady/passes/io/promote_io_variables.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "shady/ir/builtin.h"
2+
#include "shady/ir/enum.h"
23
#include "shady/ir/function.h"
34
#include "shady/ir/mem.h"
45
#include "shady/ir/debug.h"
@@ -32,7 +33,10 @@ static const Node* promote_to_physical(Context* ctx, AddressSpace as, const Node
3233
case AsPushConstant:
3334
case AsUniformConstant:
3435
case AsUInput:
35-
case AsInput: {
36+
case AsInput:
37+
case AsIncomingRayPayload:
38+
case AsRayPayload:
39+
case AsHitAttribute: {
3640
const Node* value = shd_bld_load(ctx->init_bld, io);
3741
shd_bld_store(ctx->init_bld, phy, value);
3842
// shd_bld_add_instruction(ctx->init_bld, copy_bytes_helper(a, shd_bld_mem(ctx->init_bld), phy, io, ))

test/vcc/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ shady_unit_test(COMPILER vcc NAME vcc_flat_decoration_frag FILES flat_decoration
2828
shady_unit_test(COMPILER vcc NAME vcc_checker_frag FILES checkerboard.frag.c ARGS ${VCC_TEST_ARGS} --entry-point main --execution-model Fragment VALSPV SPV_VAL_ARGS ${SHADY_SPV_VAL_VK_ARGS})
2929
shady_unit_test(COMPILER vcc NAME vcc_textured_frag FILES textured.frag.c ARGS ${VCC_TEST_ARGS} --entry-point main --execution-model Fragment VALSPV SPV_VAL_ARGS ${SHADY_SPV_VAL_VK_ARGS})
3030

31+
shady_unit_test(COMPILER vcc NAME vcc_basic_rmiss FILES basic.rmiss.c ARGS ${VCC_TEST_ARGS} --entry-point main --execution-model Fragment VALSPV SPV_VAL_ARGS ${SHADY_SPV_VAL_VK_ARGS})
32+
shady_unit_test(COMPILER vcc NAME vcc_basic_chit FILES basic.rchit.c ARGS ${VCC_TEST_ARGS} --entry-point main --execution-model Fragment VALSPV SPV_VAL_ARGS ${SHADY_SPV_VAL_VK_ARGS})
33+
3134
shady_unit_test(COMPILER vcc NAME vcc_linkage FILES linkage.frag.c lib.c ARGS ${VCC_TEST_ARGS} --entry-point main --execution-model Fragment VALSPV SPV_VAL_ARGS ${SHADY_SPV_VAL_VK_ARGS})
3235
shady_unit_test(COMPILER vcc NAME vcc_linkage_type FILES linkage2.frag.c lib2.c ARGS ${VCC_TEST_ARGS} --entry-point main --execution-model Fragment VALSPV SPV_VAL_ARGS ${SHADY_SPV_VAL_VK_ARGS})
3336

test/vcc/basic.rchit.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <shady.h>
2+
3+
#define ray_chit_shader __attribute__((annotate("shady::entry_point::RayCHit")))
4+
#define ray_payload __attribute__((annotate("shady::io::400")))
5+
#define hit_attribute __attribute__((annotate("shady::io::401")))
6+
7+
location(0) ray_payload native_vec3 hitValue;
8+
9+
hit_attribute native_vec2 attribs;
10+
11+
ray_chit_shader void main() {
12+
const native_vec3 barycentricCoords = (native_vec3){1.f - attribs.x - attribs.y, attribs.x, attribs.y};
13+
hitValue = barycentricCoords;
14+
}
15+
16+

test/vcc/basic.rmiss.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <shady.h>
2+
3+
#define ray_miss_shader __attribute__((annotate("shady::entry_point::RayMiss")))
4+
#define incoming_ray_payload __attribute__((annotate("shady::io::399")))
5+
6+
location(0) incoming_ray_payload native_vec3 hitValue;
7+
8+
ray_miss_shader void main() {
9+
hitValue = (native_vec3){0.f, 0.f, 0.2f};
10+
}
11+

0 commit comments

Comments
 (0)