Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code in RenderLayer, RenderInstance. #74

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,64 @@
#extension GL_ARB_shader_draw_parameters : enable
#extension GL_ARB_shading_language_include : enable

#include "DynamicStrandsRenderingConstants.glsl"

#include "PerFrame.glsl"
#define EE_PER_GROUP_SET 1
#include "Lighting.glsl"

layout (location = 0) in VS_OUT {
vec3 frag_pos;
vec3 normal;
vec3 tangent;
vec4 color;
vec3 FragPos;
vec3 Normal;
vec3 Tangent;
vec2 TexCoord;
vec4 Color;
} fs_in;

layout (location = 0) out vec4 out_color;

layout(location = 5) in flat uint currentInstanceIndex;

void main(){
out_color = vec4(fs_in.color);
/*
uint instanceIndex = currentInstanceIndex;

MaterialProperties materialProperties = EE_MATERIAL_PROPERTIES[EE_INSTANCES[instanceIndex].material_index];
vec2 tex_coord = fs_in.TexCoord;
vec4 albedo = materialProperties.albedo;
if (materialProperties.albedo_map_index != -1)
albedo = texture(EE_TEXTURE_2DS[materialProperties.albedo_map_index], tex_coord);
if (albedo.a <= 0.5) discard;

vec3 normal = fs_in.Normal;
if (materialProperties.normal_map_index != -1){
vec3 B = cross(fs_in.Normal, fs_in.Tangent);
mat3 TBN = mat3(fs_in.Tangent, B, fs_in.Normal);
normal = texture(EE_TEXTURE_2DS[materialProperties.normal_map_index], tex_coord).rgb;
normal = normal * 2.0 - 1.0;
normal = normalize(TBN * normal);
}

float roughness = materialProperties.roughness;
float metallic = materialProperties.metallic;
float emission = materialProperties.emission;
float ao = materialProperties.ambient_occulusion;
vec4 albedo = materialProperties.albedo;

if (materialProperties.roughness_map_index != -1) roughness = texture(EE_TEXTURE_2DS[materialProperties.roughness_map_index], materialTexCoord).r;
if (materialProperties.metallic_map_index != -1) metallic = texture(EE_TEXTURE_2DS[materialProperties.metallic_map_index], materialTexCoord).r;
if (materialProperties.ao_texture_index != -1) ao = texture(EE_TEXTURE_2DS[materialProperties.ao_texture_index], materialTexCoord).r;
if (materialProperties.albedo_map_index != -1) albedo = texture(EE_TEXTURE_2DS[materialProperties.albedo_map_index], materialTexCoord);

vec3 cameraPosition = EE_CAMERA_POSITION(EE_CAMERA_INDEX);
vec3 viewDir = normalize(cameraPosition - fragPos);
bool receiveShadow = true;
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo.xyz, metallic);
vec3 result = EE_FUNC_CALCULATE_LIGHTS(receiveShadow, albedo.xyz, 1.0, depth, normal, viewDir, fragPos, metallic, roughness, F0);
vec3 ambient = EE_FUNC_CALCULATE_ENVIRONMENTAL_LIGHT(albedo.xyz, normal, viewDir, metallic, roughness, F0);
vec3 color = result + emission * normalize(albedo.xyz) + ambient * ao;
color = pow(color, vec3(1.0 / EE_RENDER_INFO.gamma));
*/
out_color = vec4(fs_in.Color);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "PerFrame.glsl"

#define DYNAMIC_STRANDS_SET 1
#define DYNAMIC_STRANDS_SET 2
#include "DynamicStrands.glsl"

const uint WORKGROUP_SIZE = EXT_MESH_SUBGROUP_COUNT * SUBGROUP_SIZE;
Expand All @@ -24,13 +24,15 @@ const uint TETRAHEDRON_VERTEX_ITERATIONS = ((TETRAHEDRON_VERTICES_SIZE + WORK
const uint TETRAHEDRON_PRIMITIVE_ITERATIONS = ((TETRAHEDRON_TRIANGLE_SIZE + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE);

layout(location = 0) out MS_V_OUT {
vec3 frag_pos;
vec3 normal;
vec3 tangent;
vec4 color;
vec3 FragPos;
vec3 Normal;
vec3 Tangent;
vec2 TexCoord;
vec4 Color;
} ms_v_out[];

struct Task{
uint instanceIndex;
uint baseID;
uint8_t deltaIDs[EXT_TASK_WORK_GROUP_INVOCATIONS];
};
Expand All @@ -53,6 +55,8 @@ taskPayloadSharedEXT Task ts_in;
uint tet_id = ts_in.baseID + ts_in.deltaIDs[gl_WorkGroupID.x];
uint laneID = gl_LocalInvocationID.x;

layout(location = 5) out flat uint currentInstanceIndex[];

#include "AlphaShape.glsl"

void main(){
Expand All @@ -61,7 +65,7 @@ void main(){
SetMeshOutputsEXT(TETRAHEDRON_VERTICES_SIZE, delaunay_tetrahedrons[tet_id].triangles_accepted);
DelaunayTetrahedron tet = delaunay_tetrahedrons[tet_id];
// set up vertex properties
mat4 transform = EE_CAMERAS[camera_index].projection_view;
mat4 transform = EE_CAMERAS[EE_CAMERA_INDEX].projection_view;

[[unroll]]
for (uint i = 0; i < uint(TETRAHEDRON_VERTEX_ITERATIONS); ++i)
Expand All @@ -71,22 +75,24 @@ void main(){
vec3 vertex_position = uniform_particles[tet.indices[vertex_index]].position_t.xyz;
vec3 vertex_normal = normalize(uniform_particles[tet.indices[vertex_index]].normal_deg.xyz);

ms_v_out[vertex_index].frag_pos = vertex_position;
ms_v_out[vertex_index].normal = vertex_normal;
ms_v_out[vertex_index].tangent = uniform_particles[tet.indices[vertex_index]].tangent.xyz;
ms_v_out[vertex_index].FragPos = vertex_position;
ms_v_out[vertex_index].Normal = vertex_normal;
ms_v_out[vertex_index].Tangent = uniform_particles[tet.indices[vertex_index]].tangent.xyz;
ms_v_out[vertex_index].TexCoord = vec2(uniform_particles[tet.indices[vertex_index]].tex_coord.x, uniform_particles[tet.indices[vertex_index]].tex_coord.y);
if(vertex_colors == 0){
ms_v_out[vertex_index].color = vec4(0.5, 0.5, 0.5, 1.0);
ms_v_out[vertex_index].Color = vec4(0.5, 0.5, 0.5, 1.0);
} else if(vertex_colors == 1){
ms_v_out[vertex_index].color = vec4(abs(vertex_normal.xyz), 1.0f);
ms_v_out[vertex_index].Color = vec4(abs(vertex_normal.xyz), 1.0f);
} else if(vertex_colors == 2){
ms_v_out[vertex_index].color = vec4(abs(uniform_particles[tet.indices[vertex_index]].tangent.xyz), 1.0f);
ms_v_out[vertex_index].Color = vec4(abs(uniform_particles[tet.indices[vertex_index]].tangent.xyz), 1.0f);
} else if(vertex_colors == 3){
ms_v_out[vertex_index].color = vec4(mod(uniform_particles[tet.indices[vertex_index]].tex_coord.x, 1.0f),
ms_v_out[vertex_index].Color = vec4(mod(uniform_particles[tet.indices[vertex_index]].tex_coord.x, 1.0f),
mod(uniform_particles[tet.indices[vertex_index]].tex_coord.y, 1.0f),
mod(uniform_particles[tet.indices[vertex_index]].tex_coord.z, 1.0f), 1.0f);
}

gl_MeshVerticesEXT[vertex_index].gl_Position = transform * vec4(vertex_position, 1.0);
currentInstanceIndex[vertex_index] = ts_in.instanceIndex;
}

// assign all accepted triangles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#extension GL_ARB_shading_language_include : enable
#extension GL_EXT_shader_atomic_float : require

#define DYNAMIC_STRANDS_SET 1
#define DYNAMIC_STRANDS_SET 2
#include "DynamicStrands.glsl"

const uint WORKGROUP_SIZE = EXT_TASK_SUBGROUP_COUNT * SUBGROUP_SIZE;
Expand All @@ -20,6 +20,7 @@ uint laneID = gl_LocalInvocationID.x;
layout(local_size_x = WORKGROUP_SIZE) in;

struct Task{
uint instanceIndex;
uint baseID;
uint8_t deltaIDs[EXT_TASK_WORK_GROUP_INVOCATIONS];
};
Expand Down Expand Up @@ -162,6 +163,7 @@ void main(){
#endif
if (laneID == 0) {
ts_out.baseID = baseID;
ts_out.instanceIndex = gl_DrawID + EE_INSTANCE_INDEX;
}
EmitMeshTasksEXT(out_tetrahedron_count, 1, 1);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#extension GL_EXT_control_flow_attributes : require

layout(push_constant) uniform STRANDS_RENDER_CONSTANTS {
uint camera_index;
uint tetrahedrons_size;
float alpha;
float bifurcation_alpha;
int render_complex;
int vertex_colors;
};
#include "DynamicStrandsRenderingConstants.glsl"

void SortFourElements(inout uint a[4]) {
uint min1, min2, max1, max2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

layout(push_constant) uniform STRANDS_RENDER_CONSTANTS {
int EE_INSTANCE_INDEX;
int EE_CAMERA_INDEX;
uint tetrahedrons_size;
float alpha;
float bifurcation_alpha;
int render_complex;
int vertex_colors;
};
11 changes: 9 additions & 2 deletions EvoEngine_Plugins/EcoSysLab/src/DynamicStrandsRendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ void DynamicStrands::Render(const std::shared_ptr<Camera>& target_camera,

static std::shared_ptr<GraphicsPipeline> render_pipeline{};
struct RenderPushConstant {
uint32_t camera_index = 0;
int instance_index = 0;
int camera_index = 0;
uint32_t tetrahedrons_size = 0;
float alpha = 0.0f;
float bifurcation_alpha = 0.0f;
Expand Down Expand Up @@ -71,7 +72,6 @@ void DynamicStrands::Render(const std::shared_ptr<Camera>& target_camera,
ShaderType::Fragment, Platform::Constants::shader_global_defines,
std::filesystem::path("./EcoSysLabResources") / "Shaders/Graphics/Fragment/DynamicStrandsRendering.frag");
// Descriptor set layout

render_pipeline = std::make_shared<GraphicsPipeline>();
render_pipeline->task_shader = task_shader;
render_pipeline->mesh_shader = mesh_shader;
Expand All @@ -80,7 +80,10 @@ void DynamicStrands::Render(const std::shared_ptr<Camera>& target_camera,
render_pipeline->geometry_type = GeometryType::Mesh;

auto per_frame_layout = Platform::GetDescriptorSetLayout("PER_FRAME_LAYOUT");
auto lighting_layout = Platform::GetDescriptorSetLayout("LIGHTING_LAYOUT");

render_pipeline->descriptor_set_layouts.emplace_back(per_frame_layout);
render_pipeline->descriptor_set_layouts.emplace_back(lighting_layout);
render_pipeline->descriptor_set_layouts.emplace_back(strands_layout);
render_pipeline->depth_attachment_format = Platform::Constants::render_texture_depth;
render_pipeline->stencil_attachment_format = VK_FORMAT_UNDEFINED;
Expand All @@ -97,6 +100,8 @@ void DynamicStrands::Render(const std::shared_ptr<Camera>& target_camera,
const uint32_t task_work_group_invocations =
Platform::GetSelectedPhysicalDevice()->mesh_shader_properties_ext.maxPreferredTaskWorkGroupInvocations;
RenderPushConstant push_constant;
// TODO: Assign instance index here.
push_constant.instance_index = 0;
push_constant.camera_index = render_layer->GetCameraIndex(target_camera->GetHandle());
push_constant.tetrahedrons_size = delaunay_tetrahedrons.size();
push_constant.alpha = render_parameters.alpha;
Expand Down Expand Up @@ -142,6 +147,8 @@ void DynamicStrands::Render(const std::shared_ptr<Camera>& target_camera,
render_pipeline->BindDescriptorSet(vk_command_buffer, 0,
render_layer->GetPerFrameDescriptorSet()->GetVkDescriptorSet());
render_pipeline->BindDescriptorSet(vk_command_buffer, 1,
render_layer->GetLightingDescriptorSet()->GetVkDescriptorSet());
render_pipeline->BindDescriptorSet(vk_command_buffer, 2,
strands_descriptor_sets[current_frame_index]->GetVkDescriptorSet());
render_pipeline->PushConstant(vk_command_buffer, 0, push_constant);
const uint32_t count = Platform::DivUp(delaunay_tetrahedrons.size(), task_work_group_invocations);
Expand Down
1 change: 0 additions & 1 deletion EvoEngine_Plugins/EcoSysLab/src/DynamicTreeStrands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ void DynamicTreeStrands::PhysicsStep(const DynamicStrands::PhysicsParameters& ph
}
}
}

void DynamicTreeStrands::Visualization(const std::shared_ptr<Camera>& target_camera,
const DynamicStrands::VisualizationParameters& visualization_parameters) const {
if (!dynamic_strands->segments.empty()) {
Expand Down
12 changes: 1 addition & 11 deletions EvoEngine_SDK/include/Layers/RenderLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
#include "RenderInstances.hpp"
namespace evo_engine {
#pragma region Enums Structs

struct RenderInstancePushConstant {
int instance_index = 0;
int camera_index = 0;
int light_split_index = 0;
};
struct RayTracingPushConstant {
uint32_t camera_index = 0;
uint32_t frame_id = 0;
};
struct RenderInfoBlock {
glm::vec4 split_distances = {};
alignas(4) int pcf_sample_amount = 32;
Expand Down Expand Up @@ -99,7 +89,7 @@ class RenderLayer final : public ILayer {
bool cast_shadow);

[[nodiscard]] const std::shared_ptr<DescriptorSet>& GetPerFrameDescriptorSet() const;

[[nodiscard]] const std::shared_ptr<DescriptorSet>& GetLightingDescriptorSet() const;
private:
bool need_fade_ = false;
#pragma region Render procedure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ class GraphicsPipelineStates {
std::vector<VkPipelineColorBlendAttachmentState> color_blend_attachment_states = {};
float blend_constants[4] = {0, 0, 0, 0};
void ApplyAllStates(VkCommandBuffer vk_command_buffer, bool force_set = false);
void SetViewportScissor(const glm::ivec4& value, float min_depth = 0.0f, float max_depth = 1.0f);
};
} // namespace evo_engine
3 changes: 2 additions & 1 deletion EvoEngine_SDK/include/Rendering/Platform/Platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class Platform final {
static void AddBufferSyncAction(const std::string& action_name, std::function<void()>&& action);
static void RemoveBufferSyncAction(const std::string& action_name);
static void RecordCommandsMainQueue(const std::function<void(VkCommandBuffer vk_command_buffer)>& action);

static void RecordRenderCommands(const VkRenderingInfo& rendering_info, const VkCommandBuffer vk_command_buffer,
const std::function<void()>& action);
double cpu_wait_time = 0.0f;
static void WaitForDeviceIdle();

Expand Down
Loading
Loading