-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
5,725 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#version 460 | ||
#extension GL_EXT_ray_tracing : require | ||
#extension GL_EXT_nonuniform_qualifier : enable | ||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require // we need use [uint64_t] key word | ||
#extension GL_EXT_scalar_block_layout : enable | ||
#extension GL_EXT_buffer_reference2 : require | ||
|
||
struct HitPayload | ||
{ | ||
vec3 color; | ||
}; | ||
|
||
struct Vertex | ||
{ | ||
vec3 position; | ||
vec3 normal; | ||
vec2 texcoord; | ||
}; | ||
|
||
struct CallablePayload | ||
{ | ||
vec3 color; | ||
}; | ||
|
||
struct BottomLevelAccelerationStructureDeviceAddress | ||
{ | ||
uint64_t vertexDeviceAddress; | ||
uint64_t indexDeviceAddress; | ||
}; | ||
|
||
layout(location = 0) rayPayloadInEXT HitPayload HIT_PAY_LOAD; | ||
layout(location = 0) callableDataEXT CallablePayload callablePayload; | ||
|
||
layout(push_constant) uniform my_push_constants | ||
{ | ||
int callableSBTIndex; | ||
} | ||
my_push_constants_data; | ||
|
||
layout(buffer_reference, scalar) buffer Vertices | ||
{ | ||
Vertex v[]; | ||
}; | ||
|
||
layout(buffer_reference, scalar) buffer Indices | ||
{ | ||
ivec3 i[]; | ||
}; | ||
|
||
layout(set = 0, binding = 3, scalar) buffer BottomLevelAccelerationStructureDeviceAddress_ | ||
{ | ||
BottomLevelAccelerationStructureDeviceAddress blas_device_address[]; | ||
} | ||
BLAS_DEVICE_ADDRESS; | ||
|
||
hitAttributeEXT vec2 attribs; | ||
|
||
void main() | ||
{ | ||
BottomLevelAccelerationStructureDeviceAddress blas_device_address = BLAS_DEVICE_ADDRESS.blas_device_address[gl_InstanceCustomIndexEXT]; | ||
|
||
Vertices vertices = Vertices(blas_device_address.vertexDeviceAddress); | ||
Indices indices = Indices(blas_device_address.indexDeviceAddress); | ||
|
||
ivec3 index = indices.i[gl_PrimitiveID]; | ||
|
||
Vertex v0 = vertices.v[index.x]; | ||
Vertex v1 = vertices.v[index.y]; | ||
Vertex v2 = vertices.v[index.z]; | ||
|
||
const vec3 barycentrics = vec3(1.0 - attribs.x - attribs.y, attribs.x, attribs.y); | ||
|
||
const vec3 position = v0.position * barycentrics.x + v1.position * barycentrics.y + v2.position * barycentrics.z; | ||
const vec3 world_position = vec3(gl_ObjectToWorldEXT * vec4(position, 1.0)); | ||
|
||
const vec3 normal = v0.normal * barycentrics.x + v1.normal * barycentrics.y + v2.normal * barycentrics.z; | ||
const vec3 world_normal = normalize(vec3(gl_WorldToObjectEXT * vec4(normal, 1.0))); | ||
|
||
const vec3 light_dir = -normalize(vec3(1, 1, 1)); | ||
|
||
// Diffuse | ||
executeCallableEXT(my_push_constants_data.callableSBTIndex, 0); | ||
|
||
const vec3 diffuse_color = callablePayload.color; | ||
float normalDotLight = max(dot(world_normal, light_dir), 0); | ||
vec3 diffuse = diffuse_color * normalDotLight; | ||
|
||
// Specular | ||
const float PI = 3.1415925; | ||
float shininess = 28.0; | ||
float energy_conservation = (2 + shininess) / (2 * PI); | ||
vec3 view_dir = normalize(-gl_WorldRayDirectionEXT); | ||
vec3 reflect_dir = normalize(reflect(-light_dir, world_normal)); | ||
float specular_value = energy_conservation * pow(max(dot(view_dir, reflect_dir), 0), shininess); | ||
vec3 specular = vec3(specular_value); | ||
|
||
HIT_PAY_LOAD.color = diffuse + specular; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#version 460 core | ||
#extension GL_EXT_ray_tracing : enable | ||
|
||
struct CallablePayload | ||
{ | ||
vec3 color; | ||
}; | ||
|
||
layout(location = 0) callableDataInEXT CallablePayload callablePayload; | ||
|
||
void main() | ||
{ | ||
callablePayload.color = vec3(0, 0, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#version 460 core | ||
#extension GL_EXT_ray_tracing : enable | ||
|
||
struct CallablePayload | ||
{ | ||
vec3 color; | ||
}; | ||
|
||
layout(location = 0) callableDataInEXT CallablePayload callablePayload; | ||
|
||
void main() | ||
{ | ||
callablePayload.color = vec3(0, 1, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#version 460 core | ||
#extension GL_EXT_ray_tracing : enable | ||
|
||
struct CallablePayload | ||
{ | ||
vec3 color; | ||
}; | ||
|
||
layout(location = 0) callableDataInEXT CallablePayload callablePayload; | ||
|
||
void main() | ||
{ | ||
callablePayload.color = vec3(1, 0, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#version 460 | ||
#extension GL_EXT_ray_tracing : require | ||
#extension GL_EXT_nonuniform_qualifier : enable | ||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require // we need use [uint64_t] key word | ||
#extension GL_EXT_scalar_block_layout : enable | ||
#extension GL_EXT_buffer_reference2 : require | ||
|
||
#define KIND_SPHERE 0 | ||
#define KIND_CUBE 1 | ||
|
||
struct SPHERE | ||
{ | ||
vec3 center; | ||
float radius; | ||
}; | ||
|
||
struct AABB | ||
{ | ||
vec3 minimum; | ||
vec3 maximum; | ||
}; | ||
|
||
struct HitPayload | ||
{ | ||
vec3 color; | ||
}; | ||
|
||
struct Vertex | ||
{ | ||
vec3 position; | ||
vec3 normal; | ||
vec2 texcoord; | ||
}; | ||
|
||
struct BottomLevelAccelerationStructureDeviceAddress | ||
{ | ||
uint64_t vertexDeviceAddress; | ||
uint64_t indexDeviceAddress; | ||
uint64_t aabbsDeviceAddress; | ||
}; | ||
|
||
layout(location = 0) rayPayloadInEXT HitPayload HIT_PAY_LOAD; | ||
|
||
layout(buffer_reference, scalar) buffer Vertices | ||
{ | ||
Vertex v[]; | ||
}; | ||
|
||
layout(buffer_reference, scalar) buffer Indices | ||
{ | ||
ivec3 i[]; | ||
}; | ||
|
||
layout(buffer_reference, scalar) buffer AABBs | ||
{ | ||
AABB boxs[]; | ||
}; | ||
|
||
layout(set = 0, binding = 3, scalar) buffer BottomLevelAccelerationStructureDeviceAddress_ | ||
{ | ||
BottomLevelAccelerationStructureDeviceAddress blas_device_address[]; | ||
} | ||
BLAS_DEVICE_ADDRESS; | ||
|
||
hitAttributeEXT vec2 attribs; | ||
|
||
void main() | ||
{ | ||
BottomLevelAccelerationStructureDeviceAddress blas_device_address = BLAS_DEVICE_ADDRESS.blas_device_address[gl_InstanceCustomIndexEXT]; | ||
|
||
// Vertices vertices = Vertices(blas_device_address.vertexDeviceAddress); | ||
// Indices indices = Indices(blas_device_address.indexDeviceAddress); | ||
AABBs aabbs = AABBs(blas_device_address.aabbsDeviceAddress); | ||
|
||
// ivec3 index = indices.i[gl_PrimitiveID]; | ||
|
||
// Vertex v0 = vertices.v[index.x]; | ||
// Vertex v1 = vertices.v[index.y]; | ||
// Vertex v2 = vertices.v[index.z]; | ||
|
||
// const vec3 barycentrics = vec3(1.0 - attribs.x - attribs.y, attribs.x, attribs.y); | ||
|
||
// const vec3 position = v0.position * barycentrics.x + v1.position * barycentrics.y + v2.position * barycentrics.z; | ||
const vec3 world_position = gl_WorldRayOriginEXT + gl_WorldRayDirectionEXT * gl_HitTEXT; | ||
|
||
AABB aabb = aabbs.boxs[gl_PrimitiveID]; | ||
|
||
// Sphere data | ||
SPHERE sphere; | ||
sphere.center = (aabb.maximum + aabb.minimum) * 0.5; | ||
sphere.radius = ((aabb.maximum - aabb.minimum) * 0.5).x; | ||
|
||
vec3 diffuse_color = vec3(0, 1, 1); | ||
vec3 world_normal = normalize(world_position - sphere.center); | ||
if (gl_HitKindEXT == KIND_CUBE) | ||
{ | ||
diffuse_color = vec3(1, 1, 0); | ||
|
||
vec3 absN = abs(world_normal); | ||
float maxC = max(max(absN.x, absN.y), absN.z); | ||
world_normal = (maxC == absN.x) ? vec3(sign(world_normal.x), 0, 0) : (maxC == absN.y) ? vec3(0, sign(world_normal.y), 0) : vec3(0, 0, sign(world_normal.z)); | ||
} | ||
|
||
const vec3 light_dir = -normalize(vec3(1, 1, 1)); | ||
|
||
// Diffuse | ||
float normalDotLight = max(dot(world_normal, light_dir), 0); | ||
vec3 diffuse = diffuse_color * normalDotLight; | ||
|
||
// Specular | ||
const float PI = 3.1415925; | ||
float shininess = 28.0; | ||
float energy_conservation = (2 + shininess) / (2 * PI); | ||
vec3 view_dir = normalize(-gl_WorldRayDirectionEXT); | ||
vec3 reflect_dir = normalize(reflect(-light_dir, world_normal)); | ||
float specular_value = energy_conservation * pow(max(dot(view_dir, reflect_dir), 0), shininess); | ||
vec3 specular = vec3(specular_value); | ||
|
||
HIT_PAY_LOAD.color = 0.3*diffuse + specular; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#version 460 | ||
#extension GL_EXT_ray_tracing : require | ||
|
||
struct HitPayload | ||
{ | ||
vec3 color; | ||
}; | ||
|
||
layout(location = 0) rayPayloadEXT HitPayload HIT_PAY_LOAD; | ||
|
||
layout(set = 0, binding = 0) uniform accelerationStructureEXT TOP_LEVEL_AS; | ||
layout(set = 0, binding = 1, rgba32f) uniform image2D IMAGE; | ||
layout(set = 0, binding = 2) uniform RAY_TRACING_MATRIXS | ||
{ | ||
mat4 model; | ||
mat4 view; | ||
mat4 project; | ||
} | ||
rayTracingMatrixs; | ||
|
||
void main() | ||
{ | ||
const vec2 pixel_center = vec2(gl_LaunchIDEXT.xy) + vec2(0.5); // 像素中心点 | ||
const vec2 uv = pixel_center / vec2(gl_LaunchSizeEXT.xy); // 归一化 [0,1] | ||
vec2 ndc = uv * 2.0 - 1.0; //[-1,1],为归一化设备坐标(NDC) | ||
|
||
mat4 view_inverse = inverse(rayTracingMatrixs.view); | ||
mat4 project_inverse = inverse(rayTracingMatrixs.project); | ||
|
||
vec4 origin = view_inverse * vec4(0, 0, 0, 1); // 相机的世界坐标 | ||
vec4 target = project_inverse * vec4(ndc.x, ndc.y, 1, 1); // 像素中点(d.x, d.y)向前的向量乘以投影矩阵的逆得到,在View空间下该像素点的正前方向量 | ||
vec4 direction = view_inverse * vec4(normalize(target.xyz), 0); // 归一化View空间下正前方向量,之后转到世界坐标系下 | ||
|
||
uint ray_flags = gl_RayFlagsOpaqueEXT; | ||
float t_min = 0.001; | ||
float t_max = 10000.0; | ||
|
||
traceRayEXT(TOP_LEVEL_AS, // acceleration structure | ||
ray_flags, // rayFlags | ||
0xFF, // cullMask | ||
0, // sbtRecordOffset | ||
0, // sbtRecordStride | ||
0, // missIndex | ||
origin.xyz, // ray origin | ||
t_min, // ray min range | ||
direction.xyz, // ray direction | ||
t_max, // ray max range | ||
0 // payload (location = 0) | ||
); | ||
|
||
imageStore(IMAGE, ivec2(gl_LaunchIDEXT.xy), vec4(HIT_PAY_LOAD.color, 1.0)); | ||
} |
Oops, something went wrong.