Skip to content

Commit

Permalink
Support ray types
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Aug 25, 2024
1 parent fda8973 commit 7efcd7f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 9 deletions.
26 changes: 25 additions & 1 deletion Sources/backends/cstyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ static char *function_string(name_id func) {
return get_name(func);
}

// HLSL for now
static char *member_string(type *parent_type, name_id member_name) {
if (parent_type == get_type(ray_type_id)) {
if (member_name == add_name("origin")) {
return "Origin";
}
else if (member_name == add_name("direction")) {
return "Direction";
}
else if (member_name == add_name("min")) {
return "TMin";
}
else if (member_name == add_name("max")) {
return "TMax";
}
else {
return get_name(member_name);
}
}
else {
return get_name(member_name);
}
}

void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func type_string, int *indentation) {
switch (o->type) {
case OPCODE_VAR:
Expand Down Expand Up @@ -66,7 +90,7 @@ void cstyle_write_opcode(char *code, size_t *offset, opcode *o, type_string_func
else {
debug_context context = {0};
check(o->op_store_member.member_indices[i] < s->members.size, context, "Member index out of bounds");
*offset += sprintf(&code[*offset], ".%s", get_name(s->members.m[o->op_store_member.member_indices[i]].name));
*offset += sprintf(&code[*offset], ".%s", member_string(s, s->members.m[o->op_store_member.member_indices[i]].name));
is_array = s->members.m[o->op_store_member.member_indices[i]].type.array_size > 0;
s = get_type(s->members.m[o->op_store_member.member_indices[i]].type.type);
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ static char *type_string(type_id type) {
if (type == float4x4_id) {
return "float4x4";
}
if (type == ray_type_id) {
return "RayDesc";
}
if (type == bvh_type_id) {
return "RaytracingAccelerationStructure";
}
return get_name(get_type(type)->name);
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/kong.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type_ref resolve_member_var_type(statement *parent_block, type_ref parent_type,
}

debug_context context = {0};
error(context, "Member not found");
error(context, "Member %s not found", get_name(name));
type_ref t;
init_type_ref(&t, NO_NAME);
return t;
Expand Down
36 changes: 36 additions & 0 deletions Sources/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type_id function_type_id;
type_id tex2d_type_id;
type_id texcube_type_id;
type_id sampler_type_id;
type_id ray_type_id;
type_id bvh_type_id;

typedef struct prefix {
char str[5];
Expand Down Expand Up @@ -480,6 +482,40 @@ void types_init(void) {
get_type(float4x4_id)->built_in = true;
}

{
ray_type_id = add_type(add_name("ray"));
get_type(ray_type_id)->built_in = true;

type *t = get_type(ray_type_id);

debug_context context = {0};

t->members.m[t->members.size].name = add_name("origin");
t->members.m[t->members.size].type.type = float3_id;
t->members.m[t->members.size].type.array_size = 0;
++t->members.size;

t->members.m[t->members.size].name = add_name("direction");
t->members.m[t->members.size].type.type = float3_id;
t->members.m[t->members.size].type.array_size = 0;
++t->members.size;

t->members.m[t->members.size].name = add_name("min");
t->members.m[t->members.size].type.type = float_id;
t->members.m[t->members.size].type.array_size = 0;
++t->members.size;

t->members.m[t->members.size].name = add_name("max");
t->members.m[t->members.size].type.type = float_id;
t->members.m[t->members.size].type.array_size = 0;
++t->members.size;
}

{
bvh_type_id = add_type(add_name("bvh"));
get_type(bvh_type_id)->built_in = true;
}

{
function_type_id = add_type(add_name("fun"));
get_type(function_type_id)->built_in = true;
Expand Down
2 changes: 2 additions & 0 deletions Sources/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ extern type_id bool_id;
extern type_id tex2d_type_id;
extern type_id texcube_type_id;
extern type_id sampler_type_id;
extern type_id ray_type_id;
extern type_id bvh_type_id;
14 changes: 7 additions & 7 deletions tests/in/test.kong
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ fun sendrays(): void {
(1 - uv.y) * 4 - 2 + camera.y,
0);

//RayDesc ray;
//ray.Origin = camera;
//ray.Direction = target - camera;
//ray.TMin = 0.001;
//ray.TMax = 1000;
var primary: ray;
primary.origin = camera;
primary.direction = target - camera;
primary.min = 0.001;
primary.max = 1000;

var payload: Payload;
payload.allow_reflection = true;
payload.missed = false;

//TraceRay(scene, RAY_FLAG_NONE, 0xFF, 0, 0, 0, ray, payload);
//trace_ray(scene, primary, payload);

//uav[idx] = float4(payload.color, 1);
}
Expand All @@ -87,7 +87,7 @@ fun raymissed(payload: Payload): void {
}

fun closesthit(payload: Payload, uv: float2): void {

payload.color = float3(1, 0, 1);
}

#[raypipe]
Expand Down

0 comments on commit 7efcd7f

Please sign in to comment.