Skip to content

Commit

Permalink
Prepare for generalized ray tracer.
Browse files Browse the repository at this point in the history
Reduced descriptor binding for ray tracer.
Texture sampling for ray tracer.
  • Loading branch information
edisonlee0212 committed Sep 16, 2024
1 parent 5ec5110 commit e568d6c
Show file tree
Hide file tree
Showing 35 changed files with 2,030 additions and 1,618 deletions.
2 changes: 1 addition & 1 deletion EvoEngine_App/Demo/Demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Entity LoadScene(const std::shared_ptr<Scene>& scene, const std::string& base_en
scene->SetDataComponent(collection, physics_demo_transform);
#pragma endregion
}

#pragma region Create ground
auto ground = scene->CreateEntity("Ground");
std::shared_ptr<MeshRenderer> ground_mesh_renderer;
Expand Down
1 change: 0 additions & 1 deletion EvoEngine_App/EcoSysLab/LogGraderApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ int main() {
editorLayer->enable_gizmos = false;
editorLayer->GetSceneCamera()->clear_color = glm::vec3(1.f);
const auto renderLayer = Application::GetLayer<RenderLayer>();
renderLayer->enable_particles = false;

ProjectManager::GetInstance().show_project_window = false;
#pragma region Engine Loop
Expand Down
6 changes: 3 additions & 3 deletions EvoEngine_Plugins/LogScanning/src/JoeScanScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ bool JoeScan::OnInspect(const std::shared_ptr<EditorLayer>& editorLayer) {
const auto newEntity = scene->CreateEntity("Scan");
const auto particles = scene->GetOrSetPrivateComponent<Particles>(newEntity).lock();
const auto material = ProjectManager::CreateTemporaryAsset<Material>();
particles->m_material = material;
particles->m_mesh = Resources::GetResource<Mesh>("PRIMITIVE_CUBE");
particles->m_particleInfoList = joeScanList;
particles->material = material;
particles->mesh = Resources::GetResource<Mesh>("PRIMITIVE_CUBE");
particles->particle_info_list = joeScanList;
*/
}
}
Expand Down
1 change: 1 addition & 0 deletions EvoEngine_SDK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(EVOENGINE_SDK_INCLUDES
${EVOENGINE_SDK_DIR}/include/Physics
${EVOENGINE_SDK_DIR}/include/RayTracing
${EVOENGINE_SDK_DIR}/include/Rendering
${EVOENGINE_SDK_DIR}/include/Rendering/RenderInstances
${EVOENGINE_SDK_DIR}/include/Rendering/PBR
${EVOENGINE_SDK_DIR}/include/Rendering/Animation
${EVOENGINE_SDK_DIR}/include/Rendering/Renderer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,84 @@
#extension GL_ARB_shading_language_include : enable

#define SCENE_LEVEL_BVH_BLOCK_BINDING 0
#define NODE_INDICES_BLOCK_BINDING 1
#define NODE_INFO_LIST_BLOCK_BINDING 2
#define NODE_LEVEL_BVH_NODES_BLOCK_BINDING 3
#define MESH_INDICES_BLOCK_BINDING 4
#define MESH_INFO_LIST_BLOCK_BINDING 5
#define MESH_LEVEL_BVH_NODES_BLOCK_BINDING 6
#define TRIANGLE_INDICES_BLOCK_BINDING 7
#define LOCAL_TRIANGLE_INDICES_BLOCK_BINDING 8
#define SCENE_TRIANGLES_BLOCK_BINDING 9
#define SCENE_VERTEX_POSITIONS_BLOCK_BINDING 10
#define SCENE_INFO_BLOCK_BINDING 11
#extension GL_EXT_nonuniform_qualifier : enable
#define TRACE_DATA_SET 0
#define EE_PER_FRAME_SET 0
#define SCENE_GRAPH_DATA_BINDING 0
#define SCENE_GEOMETRY_DATA_BINDING 1
#define SCENE_INFO_BLOCK_BINDING 2
#include "Trace.glsl"

#define EE_ENVIRONMENTAL_BLOCK_BINDING 12
#include "Environment.glsl"

#define EE_MATERIAL_BLOCK_BINDING 13
#define EE_MATERIAL_BLOCK_BINDING 3
#include "Materials.glsl"

#define EE_INSTANCE_BLOCK_BINDING 14
#define EE_INSTANCE_BLOCK_BINDING 4
#include "Instances.glsl"

#define EE_DIRECTIONAL_LIGHT_BLOCK_BINDING 15
#define EE_POINT_LIGHT_BLOCK_BINDING 16
#define EE_SPOT_LIGHT_BLOCK_BINDING 17
#include "Lights.glsl"

#define EE_TEXTURE_2DS_BINDING 18
#define EE_CUBEMAPS_BINDING 19
#define EE_TEXTURE_2DS_BINDING 6
#define EE_CUBEMAPS_BINDING 7
#include "Textures.glsl"

#include "Random.glsl"

layout(set = TRACE_DATA_SET, binding = 5, rgba32f) uniform image2D result_image;

layout(push_constant) uniform RAY_CASTING_CONSTANTS{
vec4 resolution_xy_sample_bounce;
mat4 inverse_projection_view;
};
void Execute(uint pixel_index);

layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;

#define FLT_MAX 3.402823466e+38

void main() {
uint pixel_index = gl_GlobalInvocationID.x;
uint seed = pixel_index;
Execute(pixel_index);
}

void Execute(uint pixel_index) {
uint resolution_x = floatBitsToUint(resolution_xy_sample_bounce.x);
uint resolution_y = floatBitsToUint(resolution_xy_sample_bounce.y);
uint sample_count = floatBitsToUint(resolution_xy_sample_bounce.z);
uint bounce_count = floatBitsToUint(resolution_xy_sample_bounce.w);

float x_coordinate = float(pixel_index % resolution_x);
float y_coordinate = float(pixel_index % resolution_y);
uint seed = pixel_index;
if(pixel_index < resolution_x * resolution_y){
uint sample_count = floatBitsToUint(resolution_xy_sample_bounce.z);
uint bounce_count = floatBitsToUint(resolution_xy_sample_bounce.w);

float half_x = float(resolution_x) * .5;
float half_y = float(resolution_y) * .5;
float x_coordinate = float(pixel_index % resolution_y);
float y_coordinate = float(pixel_index / resolution_y);

for(uint sample_index = 0; sample_index < sample_count; sample_index += 1){
vec2 screen = vec2((x_coordinate + EE_RANDOM(seed) - half_x) / half_x,
(y_coordinate + EE_RANDOM(seed) - half_y) / half_y);
vec4 start = inverse_projection_view * vec4(screen.x, screen.y, 0.0, 1.0);
vec4 end = inverse_projection_view * vec4(screen.x, screen.y, 1.0, 1.0);
start /= start.w;
end /= end.w;
float half_x = float(resolution_x) * .5;
float half_y = float(resolution_y) * .5;
RayDescriptor ray_descriptor;
ray_descriptor.origin = start.xyz;
ray_descriptor.direction = normalize(end.xyz - start.xyz);
ray_descriptor.t_min = 0.;
ray_Dexcriptor.t_max = FLT_MAX;
HitInfo hit_info = Trace(ray_descriptor, true, false, hit);
ray_descriptor.t_max = FLT_MAX;

vec4 color;
float screen_x = (x_coordinate + EE_RANDOM(seed) - half_x) / half_x;
float screen_y = (y_coordinate + EE_RANDOM(seed) - half_y) / half_y;
vec4 camera_start = inverse_projection_view * vec4(screen_x, screen_y, 0.0, 1.0);
vec4 camera_end = inverse_projection_view * vec4(screen_x, screen_y, 1.0, 1.0);
camera_start /= camera_start.w;
camera_end /= camera_end.w;
vec3 sample_origin = camera_start.xyz;
vec3 sample_direction = normalize(camera_end.xyz - camera_start.xyz);
bool hit = false;
ray_descriptor.origin = sample_origin;
ray_descriptor.direction = sample_direction;
HitInfo hit_info = Trace(ray_descriptor, false, false, hit);
if (hit) {

uint material_index = EE_INSTANCES[hit_info.instance_index].material_index;
MaterialProperties mat_props = EE_MATERIAL_PROPERTIES[material_index];
Vertex p0, p1, p2;
GetTriangle(hit_info.triangle_index, p0, p1, p2);
vec2 tex_coord = hit_info.barycentric.x * p0.tex_coord + hit_info.barycentric.y * p1.tex_coord + hit_info.barycentric.z * p2.tex_coord;
vec4 albedo = mat_props.albedo;
if (mat_props.albedo_map_index != -1) {
albedo = texture(EE_TEXTURE_2DS[mat_props.albedo_map_index], tex_coord);
}
color = albedo;
}
vec4 accumlated = imageLoad(result_image, ivec2(x_coordinate, y_coordinate)) * sample_count;
imageStore(result_image, ivec2(x_coordinate, y_coordinate), (color + accumlated) / (sample_count + 1));
}
}
27 changes: 12 additions & 15 deletions EvoEngine_SDK/Internals/DefaultResources/Shaders/Compute/Trace.comp
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
#extension GL_ARB_shading_language_include : enable

#define SCENE_LEVEL_BVH_BLOCK_BINDING 0
#define NODE_INDICES_BLOCK_BINDING 1
#define NODE_INFO_LIST_BLOCK_BINDING 2
#define NODE_LEVEL_BVH_NODES_BLOCK_BINDING 3
#define MESH_INDICES_BLOCK_BINDING 4
#define MESH_INFO_LIST_BLOCK_BINDING 5
#define MESH_LEVEL_BVH_NODES_BLOCK_BINDING 6
#define TRIANGLE_INDICES_BLOCK_BINDING 7
#define LOCAL_TRIANGLE_INDICES_BLOCK_BINDING 8
#define SCENE_TRIANGLES_BLOCK_BINDING 9
#define SCENE_VERTEX_POSITIONS_BLOCK_BINDING 10
#define SCENE_INFO_BLOCK_BINDING 11
#define TRACE_DATA_SET 0
#define SCENE_GRAPH_DATA_BINDING 0
#define SCENE_GEOMETRY_DATA_BINDING 1
#define SCENE_INFO_BLOCK_BINDING 2
#include "Trace.glsl"

layout(std140, binding = 12) readonly buffer RAYS_BLOCK{
layout(set = TRACE_DATA_SET, binding = 3) readonly buffer RAYS_BLOCK{
vec4 rays[];
};

layout(std140, binding = 13) writeonly buffer RAY_CASTING_RESULTS_BLOCK{
layout(set = TRACE_DATA_SET, binding = 4) writeonly buffer RAY_CASTING_RESULTS_BLOCK{
vec4 ray_casting_results[];
};

Expand Down Expand Up @@ -51,7 +43,12 @@ void Execute(uint current_ray_index) {
ray_casting_results[current_ray_index * 4] = vec4(hit_info.hit, 1);
ray_casting_results[current_ray_index * 4 + 1] = vec4(hit_info.barycentric, hit_info.back_face ? 1.0 : 0.0);
ray_casting_results[current_ray_index * 4 + 2] = vec4(hit_info.normal, hit_info.hit_dist);
ray_casting_results[current_ray_index * 4 + 3] = vec4(uintBitsToFloat(hit_info.node_index), uintBitsToFloat(hit_info.mesh_index), uintBitsToFloat(hit_info.triangle_index), 0.0);
ray_casting_results[current_ray_index * 4 + 3] = vec4(uintBitsToFloat(hit_info.instance_index), uintBitsToFloat(hit_info.node_index), uintBitsToFloat(hit_info.mesh_index), uintBitsToFloat(hit_info.local_triangle_index));
}else {
ray_casting_results[current_ray_index * 4] = vec4(0, 0, 0, 0);
ray_casting_results[current_ray_index * 4 + 1] = vec4(0, 0, 0, 0);
ray_casting_results[current_ray_index * 4 + 2] = vec4(0, 0, 0, 0);
ray_casting_results[current_ray_index * 4 + 3] = vec4(0, 0, 0, 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require
#extension GL_ARB_shading_language_include : enable

#define EE_PER_FRAME_SET 0
#define EE_PER_PASS_SET 1
Expand Down Expand Up @@ -61,15 +62,7 @@ layout(set = EE_PER_FRAME_SET, binding = 5) uniform EE_KERNEL_BLOCK {
#define EE_SPOT_LIGHT_BLOCK_BINDING 8
#include "Lights.glsl"

struct Vertex {
vec3 position;
vec3 normal;
vec3 tangent;

vec4 color;
vec2 tex_coord;
vec2 vertex_info;
};
#include "Vertex.glsl"

struct VertexDataChunk {
Vertex vertices[MESHLET_MAX_VERTICES_SIZE];
Expand Down Expand Up @@ -114,7 +107,7 @@ layout(set = EE_PER_PASS_SET, binding = 18) readonly buffer EE_INSTANCED_DATA_BL
int EE_STRANDS_SEGMENT_SUBDIVISION(in vec3 worldPosA, in vec3 worldPosB) {
vec4 coordA = EE_CAMERAS[EE_CAMERA_INDEX].projection_view * vec4(worldPosA, 1.0);
vec4 coordB = EE_CAMERAS[EE_CAMERA_INDEX].projection_view * vec4(worldPosB, 1.0);
vec2 screenSize = vec2(EE_CAMERA_RESOLUTION_X(EE_CAMERA_INDEX), EE_CAMERA_RESOLUTION_Y(EE_CAMERA_INDEX));
vec2 screenSize = vec2(EE_CAMERA_RESOLUTION_X(EE_CAMERA_INDEX), EE_CAMERA_RESOLUTION_Y(EE_CAMERA_INDEX));
coordA = coordA / coordA.w;
coordB = coordB / coordB.w;
if (coordA.z < -1.0 && coordB.z < -1.0) return 0;
Expand Down
Loading

0 comments on commit e568d6c

Please sign in to comment.