Skip to content

Commit

Permalink
dev merge to master
Browse files Browse the repository at this point in the history
  • Loading branch information
FuXiii committed Nov 4, 2023
2 parents 1fb6353 + dd80ebb commit bf1838c
Show file tree
Hide file tree
Showing 21 changed files with 5,725 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(Turbo)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Wunused-function")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-unused-function -Wno-switch")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -Werror -Wno-switch -Wno-missing-field-initializers -Wno-unused-variable -Wno-unused-parameter -Wno-unused-function")

set(CMAKE_BUILD_TYPE "debug")
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3661,3 +3661,33 @@ git clone --recursive git@github.com:FuXiii/Turbo.git
>* `./engine/core`下`TShader`类析构中增加对`std::vector<TStorageBufferDescriptor *> storageBufferDescriptors`成员的内存释放。
>* `./engine/core`下`TShader`类中增加`const std::vector<TStorageBufferDescriptor *> &GetStorageBufferDescriptors()`成员函数。用于获取着色器中的对应描述符信息。
>* `./engine/core`下`TPipeline`类中`InternalCreate()`成员函数中增加对于`std::vector<TStorageBufferDescriptor *>`描述符的处理。
* 2023/10/11 设计架构
>
>* `./samples`下增加`VulkanKHRRayTracingTestForIntersectionShader`示例。用于研究光追中相交着色器的使用。
>* `./asset/shaders`下新增`RayTracingKHRTestForIntersection.rgen`文件,用于`VulkanKHRRayTracingTestForIntersectionShader`示例。
>* `./asset/shaders`下新增`RayTracingKHRTestForIntersection.rchit`文件,用于`VulkanKHRRayTracingTestForIntersectionShader`示例。
>* `./asset/shaders`下新增`RayTracingKHRTestForIntersection.rmiss`文件,用于`VulkanKHRRayTracingTestForIntersectionShader`示例。
>* `./asset/shaders`下新增`RayTracingKHRTestForIntersection.rint`文件,用于`VulkanKHRRayTracingTestForIntersectionShader`示例。
* 2023/10/29 设计架构
>
>* 配了一台`3060Ti`显卡的电脑,光追研究继续。
>* `./engine/render`下`TBuffer.h`中`TUniformBuffer`移除`std::enable_if_t`(`llvm-mingw`编译器不支持,估计是`C++`标准配置不对)。
>* `./thirdparty`下更新`KTX-Software`到最新`main`分支。
>* `./engine/render`下`TContext`中移除对于`TDescriptorID`的初始化列表写法使用最原始的结构体赋值法。(`llvm-mingw`好像不支持初始化列表写法,目前不知道如何设置编译器支持初始化列表写法,估计是`C++`标准配置不对)。
>* `./engine/render`下`TContext`中将`std::is_class_v<A_Test>`改为`std::is_class<A_Test>::value`。
>* `./samples`下`VulkanTest`中将所有`std::exception(const std::string&)`改为`Turbo::Core::TException`。
>* `./samples`下`VulkanAllocatorTest`中将所有`std::exception(const std::string&)`改为`Turbo::Core::TException`。
* 2023/10/31 设计架构
>
>* `./engine/render`下`TBuffer.h`中`TUniformBuffer`移除`std::enable_if_t`(`C++14`特性),使用`std::enable_if`替换(`C++11`特性)。
* 2023/11/4 设计架构
>
>* `./samples`下增加`VulkanKHRRayTracingTestForCallableShader`示例。用于研究可调用着色器的使用。
>* `./asset/shaders`下新增`RayTracingKHRTestForCallableShader.rchit`文件,用于`VulkanKHRRayTracingTestForCallableShader`示例。
>* `./asset/shaders`下新增`RayTracingKHRTestForCallableShader_R.rchit`文件,用于`VulkanKHRRayTracingTestForCallableShader`示例。
>* `./asset/shaders`下新增`RayTracingKHRTestForCallableShader_G.rchit`文件,用于`VulkanKHRRayTracingTestForCallableShader`示例。
>* `./asset/shaders`下新增`RayTracingKHRTestForCallableShader_B.rchit`文件,用于`VulkanKHRRayTracingTestForCallableShader`示例。
98 changes: 98 additions & 0 deletions asset/shaders/RayTracingKHRTestForCallableShader.rchit
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;
}
14 changes: 14 additions & 0 deletions asset/shaders/RayTracingKHRTestForCallableShader_B.rcall
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);
}
14 changes: 14 additions & 0 deletions asset/shaders/RayTracingKHRTestForCallableShader_G.rcall
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);
}
14 changes: 14 additions & 0 deletions asset/shaders/RayTracingKHRTestForCallableShader_R.rcall
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);
}
120 changes: 120 additions & 0 deletions asset/shaders/RayTracingKHRTestForIntersection.rchit
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;
}
52 changes: 52 additions & 0 deletions asset/shaders/RayTracingKHRTestForIntersection.rgen
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));
}
Loading

0 comments on commit bf1838c

Please sign in to comment.