adding anyhit, doesnt work though

This commit is contained in:
Dynamitos
2025-01-30 23:25:41 +01:00
parent 44b147084b
commit 89570ff792
19 changed files with 136 additions and 70 deletions
-2
View File
@@ -32,7 +32,5 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
} }
result += brdf.evaluateAmbient(); result += brdf.evaluateAmbient();
// gamma correction // gamma correction
//result = result / (result + float3(1.0));
//result = pow(result, float3(1.0/2.2));
return float4(result, brdf.getAlpha()); return float4(result, brdf.getAlpha());
} }
+73 -21
View File
@@ -2,10 +2,13 @@ import Common;
interface IBRDF interface IBRDF
{ {
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor); float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor);
[mutating] void transformNormal(float3x3 tangentToWorld);
float3 getNormal();
float3 getBaseColor();
float3 evaluateAmbient(); float3 evaluateAmbient();
float getAlpha(); float getAlpha();
float3 getTangentNormal(); float3 getEmissive();
}; };
struct Phong : IBRDF struct Phong : IBRDF
@@ -29,16 +32,28 @@ struct Phong : IBRDF
emissive = float3(0, 0, 0); emissive = float3(0, 0, 0);
} }
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor) float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{ {
float3 normal_WS = normalize(mul(tangentToWorld, normal)); float3 normal_WS = normal;
float3 nDotL = dot(normal_WS, lightDir_WS); float3 nDotL = dot(normal_WS, lightDir_WS);
float3 r = 2 * (nDotL) * normal_WS - lightDir_WS; float3 r = 2 * (nDotL) * normal_WS - lightDir_WS;
float rDotV = dot(r, viewDir_WS); float rDotV = dot(r, viewDir_WS);
return lightColor * (baseColor * max(nDotL, 0.0)) + specular * pow(max(rDotV, 0.0), max(shininess, 1)); return lightColor * (baseColor * max(nDotL, 0.0)) + specular * pow(max(rDotV, 0.0), max(shininess, 1));
} }
[mutating]
void transformNormal(float3x3 tangentToWorld)
{
normal = normalize(mul(tangentToWorld, normal));
}
float3 getNormal()
{
return normal;
}
float3 getBaseColor()
{
return baseColor;
}
float3 evaluateAmbient() float3 evaluateAmbient()
{ {
return ambient; return ambient;
@@ -47,9 +62,9 @@ struct Phong : IBRDF
{ {
return alpha; return alpha;
} }
float3 getTangentNormal() float3 getEmissive()
{ {
return normal; return emissive;
} }
}; };
@@ -74,16 +89,28 @@ struct BlinnPhong : IBRDF
emissive = float3(0, 0, 0); emissive = float3(0, 0, 0);
} }
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor) float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{ {
float3 normal_WS = normalize(mul(tangentToWorld, normal)); float3 normal_WS = normal;
float diffuse = max(dot(normal_WS, lightDir_WS), 0); float diffuse = max(dot(normal_WS, lightDir_WS), 0);
float3 h = normalize(lightDir_WS + viewDir_WS); float3 h = normalize(lightDir_WS + viewDir_WS);
float specular = pow(saturate(dot(normal_WS, h)), shininess); float specular = pow(saturate(dot(normal_WS, h)), shininess);
return (baseColor * diffuse * lightColor) + (specularColor * specular); return (baseColor * diffuse * lightColor) + (specularColor * specular);
} }
[mutating]
void transformNormal(float3x3 tangentToWorld)
{
normal = normalize(mul(tangentToWorld, normal));
}
float3 getNormal()
{
return normal;
}
float3 getBaseColor()
{
return baseColor;
}
float3 evaluateAmbient() float3 evaluateAmbient()
{ {
return ambient; return ambient;
@@ -92,9 +119,9 @@ struct BlinnPhong : IBRDF
{ {
return alpha; return alpha;
} }
float3 getTangentNormal() float3 getEmissive()
{ {
return normal; return emissive;
} }
}; };
@@ -112,10 +139,9 @@ struct CelShading : IBRDF
normal = float3(0, 0, 1); normal = float3(0, 0, 1);
emissive = float3(0, 0, 0); emissive = float3(0, 0, 0);
} }
float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{ {
float3 normal_WS = normalize(mul(tangentToWorld, normal)); float3 normal_WS = normal;
float nDotL = dot(normal_WS, lightDir_WS); float nDotL = dot(normal_WS, lightDir_WS);
float diffuse = max(nDotL, 0); float diffuse = max(nDotL, 0);
@@ -129,6 +155,19 @@ struct CelShading : IBRDF
return darkenedBase * lightColor; return darkenedBase * lightColor;
} }
} }
[mutating]
void transformNormal(float3x3 tangentToWorld)
{
normal = normalize(mul(tangentToWorld, normal));
}
float3 getNormal()
{
return normal;
}
float3 getBaseColor()
{
return baseColor;
}
float3 evaluateAmbient() float3 evaluateAmbient()
{ {
return float3(0, 0, 0); return float3(0, 0, 0);
@@ -137,9 +176,9 @@ struct CelShading : IBRDF
{ {
return alpha; return alpha;
} }
float3 getTangentNormal() float3 getEmissive()
{ {
return normal; return emissive;
} }
}; };
@@ -197,9 +236,9 @@ struct CookTorrance : IBRDF
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
} }
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor) float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{ {
float3 n = normalize(mul(tangentToWorld, normal)); float3 n = normal;
float3 h = normalize(lightDir_WS + viewDir_WS); float3 h = normalize(lightDir_WS + viewDir_WS);
float3 F0 = float3(0.04); float3 F0 = float3(0.04);
@@ -224,6 +263,19 @@ struct CookTorrance : IBRDF
float3 result = (k_d * baseColor / PI + specular) * nDotL * lightColor; float3 result = (k_d * baseColor / PI + specular) * nDotL * lightColor;
return result * ambientOcclusion; return result * ambientOcclusion;
} }
[mutating]
void transformNormal(float3x3 tangentToWorld)
{
normal = normalize(mul(tangentToWorld, normal));
}
float3 getNormal()
{
return normal;
}
float3 getBaseColor()
{
return baseColor;
}
float3 evaluateAmbient() float3 evaluateAmbient()
{ {
return float3(0.03) * baseColor * ambientOcclusion; return float3(0.03) * baseColor * ambientOcclusion;
@@ -232,8 +284,8 @@ struct CookTorrance : IBRDF
{ {
return alpha; return alpha;
} }
float3 getTangentNormal() float3 getEmissive()
{ {
return normal; return emissive;
} }
}; };
+2 -2
View File
@@ -15,7 +15,7 @@ struct DirectionalLight : ILightEnv
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf) float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
{ {
float3 dir_WS = -normalize(direction.xyz); float3 dir_WS = -normalize(direction.xyz);
return brdf.evaluate(params.tangentToWorld, params.viewDir_WS, dir_WS, color.xyz); return brdf.evaluate(params.viewDir_WS, dir_WS, color.xyz);
} }
}; };
@@ -29,7 +29,7 @@ struct PointLight : ILightEnv
float3 lightDir_WS = position_WS.xyz - params.position_WS; float3 lightDir_WS = position_WS.xyz - params.position_WS;
float d = length(lightDir_WS); float d = length(lightDir_WS);
float illuminance = max(1 - d / colorRange.w, 0); float illuminance = max(1 - d / colorRange.w, 0);
return illuminance * brdf.evaluate(params.tangentToWorld, params.viewDir_WS, normalize(lightDir_WS), colorRange.xyz); return illuminance * brdf.evaluate(params.viewDir_WS, normalize(lightDir_WS), colorRange.xyz);
} }
bool insidePlane(Plane plane, float3 position) bool insidePlane(Plane plane, float3 position)
+2 -2
View File
@@ -5,8 +5,8 @@ import Scene;
//interface IMaterial //interface IMaterial
//{ //{
// associatedtype BRDF : IBRDF; // associatedtype BRDF: IBRDF;
// BRDF prepare(MaterialParameter input); // static BRDF prepare(MaterialParameter input);
//}; //};
struct MaterialResources struct MaterialResources
{ {
+2 -2
View File
@@ -2,6 +2,7 @@ import Common;
struct MaterialParameter struct MaterialParameter
{ {
float3x3 tangentToWorld;
float3 position_WS; float3 position_WS;
float2 texCoords[MAX_TEXCOORDS]; float2 texCoords[MAX_TEXCOORDS];
float3 vertexColor; float3 vertexColor;
@@ -9,7 +10,6 @@ struct MaterialParameter
struct LightingParameter struct LightingParameter
{ {
float3x3 tangentToWorld;
float3 viewDir_WS; float3 viewDir_WS;
float3 position_WS; float3 position_WS;
} }
@@ -72,6 +72,7 @@ struct FragmentParameter
MaterialParameter getMaterialParameter() MaterialParameter getMaterialParameter()
{ {
MaterialParameter result; MaterialParameter result;
result.tangentToWorld = getTangentToWorld();
result.position_WS = position_WS; result.position_WS = position_WS;
result.texCoords[0] = texCoords0.xy; result.texCoords[0] = texCoords0.xy;
result.texCoords[1] = texCoords0.zw; result.texCoords[1] = texCoords0.zw;
@@ -88,7 +89,6 @@ struct FragmentParameter
LightingParameter getLightingParameter() LightingParameter getLightingParameter()
{ {
LightingParameter result; LightingParameter result;
result.tangentToWorld = getTangentToWorld();
result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS); result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS);
result.position_WS = position_WS; result.position_WS = position_WS;
return result; return result;
+4 -4
View File
@@ -6,7 +6,7 @@ struct VertexInput
uint instanceId: SV_InstanceID; uint instanceId: SV_InstanceID;
} }
interface IVertexData //interface IVertexData
{ //{
VertexAttributes getAttributes(uint index); // VertexAttributes getAttributes(uint index);
}; //};
+4 -1
View File
@@ -5,7 +5,7 @@ import MaterialParameter;
import StaticMeshVertexData; import StaticMeshVertexData;
[shader("anyhit")] [shader("anyhit")]
void anyhit(inout RayPayload hitvalue, in BuiltInTriangleIntersectionAttributes attr) void anyhit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
{ {
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y); const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
@@ -31,4 +31,7 @@ void anyhit(inout RayPayload hitvalue, in BuiltInTriangleIntersectionAttributes
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords); FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
hitValue.params = params.getLightingParameter();
hitValue.materialParams = params.getMaterialParameter();
AcceptHitAndEndSearch();
} }
+12 -9
View File
@@ -83,6 +83,7 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
// todo: replace with anyhit shader // todo: replace with anyhit shader
if(hitValue.anyHit) if(hitValue.anyHit)
return; return;
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y); const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
InstanceData inst = pScene.instances[InstanceID()]; InstanceData inst = pScene.instances[InstanceID()];
@@ -107,14 +108,15 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords); FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
MaterialParameter materialParams = params.getMaterialParameter(); hitValue.params = params.getLightingParameter();
LightingParameter lightingParams = params.getLightingParameter(); hitValue.materialParams = params.getMaterialParameter();
LightingParameter lightingParams = hitValue.params;
lightingParams.viewDir_WS = -WorldRayDirection(); lightingParams.viewDir_WS = -WorldRayDirection();
let brdf = Material.prepare(materialParams); let brdf = Material.prepare(hitValue.materialParams);
float3 normal_WS = normalize(mul(params.getTangentToWorld(), brdf.normal)); float3 normal_WS = brdf.getNormal();
float3 normalLight_WS = dot(normal_WS,WorldRayDirection())<0 ? normal_WS : -normal_WS; float3 normalLight_WS = dot(normal_WS,WorldRayDirection())<0 ? normal_WS : -normal_WS;
float3 intersection_WS = params.position_WS; float3 intersection_WS = lightingParams.position_WS;
hitValue.depth++; hitValue.depth++;
float3 rnd = rand01(hitValue.rndSeed); float3 rnd = rand01(hitValue.rndSeed);
@@ -139,12 +141,12 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
// TraceRay(scene, 0, 0xff, 0, 0, rayDesc); // TraceRay(scene, 0, 0xff, 0, 0, rayDesc);
//} //}
float p = max(max(brdf.baseColor.x, brdf.baseColor.y), brdf.baseColor.z); float p = max(max(brdf.getBaseColor().x, brdf.getBaseColor().y), brdf.getBaseColor().z);
if(hitValue.depth > 5) { if(hitValue.depth > 5) {
if (rnd.z >= p) return; if (rnd.z >= p) return;
} }
hitValue.light += brdf.emissive * hitValue.emissive + brdf.evaluateAmbient(); hitValue.light += brdf.getEmissive() * hitValue.emissive + brdf.evaluateAmbient();
//-- Ideal DIFFUSE reflection //-- Ideal DIFFUSE reflection
//if(bool(useNEE)) { //if(bool(useNEE)) {
// accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd); // accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd);
@@ -211,7 +213,8 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
payload.rndSeed = hitValue.rndSeed + 1; payload.rndSeed = hitValue.rndSeed + 1;
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xff, 0, 0, 0, rayDesc, payload); TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xff, 0, 0, 0, rayDesc, payload);
float bias = dot(normalLight_WS, rayDesc.Direction); float bias = dot(normalLight_WS, rayDesc.Direction);
hitValue.light += brdf.evaluate(params.getTangentToWorld(), -WorldRayDirection(), rayDesc.Direction, payload.light / bias); hitValue.light += brdf.evaluate(-WorldRayDirection(), rayDesc.Direction, payload.light / bias);
} }
hitValue.light /= p; //hitValue.light /= p;
} }
+3 -6
View File
@@ -1,4 +1,5 @@
import MaterialParameter; import MaterialParameter;
import BRDF;
struct RayTracingParams struct RayTracingParams
{ {
@@ -12,14 +13,10 @@ struct RayTracingParams
layout(set=5) layout(set=5)
ParameterBlock<RayTracingParams> pRayTracingParams; ParameterBlock<RayTracingParams> pRayTracingParams;
struct CallablePayload
{
FragmentParameter params;
float3 color;
};
struct RayPayload struct RayPayload
{ {
LightingParameter params;
MaterialParameter materialParams;
float3 light; float3 light;
float emissive; float emissive;
uint depth; uint depth;
+2 -2
View File
@@ -172,7 +172,7 @@ void BasePass::render() {
// LightEnv => provided by scene // LightEnv => provided by scene
// Material => per material // Material => per material
// LightCulling => calculated by pass // LightCulling => calculated by pass
permutation.setMaterial(materialData.material->getName()); permutation.setMaterial(materialData.material->getName(), materialData.material->getProfile());
Gfx::PermutationId id(permutation); Gfx::PermutationId id(permutation);
Gfx::ORenderCommand command = graphics->createRenderCommand("BaseRender"); Gfx::ORenderCommand command = graphics->createRenderCommand("BaseRender");
@@ -273,7 +273,7 @@ void BasePass::render() {
transparentCommand->setViewport(viewport); transparentCommand->setViewport(viewport);
for (const auto& [_, t] : sortedDraws) { for (const auto& [_, t] : sortedDraws) {
permutation.setVertexData(t.vertexData->getTypeName()); permutation.setVertexData(t.vertexData->getTypeName());
permutation.setMaterial(t.matInst->getBaseMaterial()->getName()); permutation.setMaterial(t.matInst->getBaseMaterial()->getName(), t.matInst->getBaseMaterial()->getProfile());
Gfx::PermutationId id(permutation); Gfx::PermutationId id(permutation);
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id); const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id);
@@ -87,7 +87,7 @@ void RayTracingPass::render() {
PMaterial mat = matData.material; PMaterial mat = matData.material;
Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("RayTracing"); Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("RayTracing");
permutation.setMaterial(mat->getName()); permutation.setMaterial(mat->getName(), mat->getProfile());
permutation.setVertexData(vertexData->getTypeName()); permutation.setVertexData(vertexData->getTypeName());
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(Gfx::PermutationId(permutation)); const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(Gfx::PermutationId(permutation));
@@ -97,6 +97,7 @@ void RayTracingPass::render() {
for (uint32 i = 0; i < inst.instanceData.size(); ++i) { for (uint32 i = 0; i < inst.instanceData.size(); ++i) {
Gfx::RayTracingHitGroup callableGroup = { Gfx::RayTracingHitGroup callableGroup = {
.closestHitShader = collection->callableShader, .closestHitShader = collection->callableShader,
.anyHitShader = anyhit,
}; };
callableGroup.parameters.resize(sizeof(VertexData::DrawCallOffsets)); callableGroup.parameters.resize(sizeof(VertexData::DrawCallOffsets));
std::memcpy(callableGroup.parameters.data(), &inst.offsets, sizeof(VertexData::DrawCallOffsets)); std::memcpy(callableGroup.parameters.data(), &inst.offsets, sizeof(VertexData::DrawCallOffsets));
@@ -111,12 +112,13 @@ void RayTracingPass::render() {
PMaterial mat = transparentData.matInst->getBaseMaterial(); PMaterial mat = transparentData.matInst->getBaseMaterial();
Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("RayTracing"); Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("RayTracing");
permutation.setMaterial(mat->getName()); permutation.setMaterial(mat->getName(), mat->getProfile());
permutation.setVertexData(vertexData->getTypeName()); permutation.setVertexData(vertexData->getTypeName());
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(Gfx::PermutationId(permutation)); const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(Gfx::PermutationId(permutation));
assert(collection != nullptr); assert(collection != nullptr);
Gfx::RayTracingHitGroup callableGroup = { Gfx::RayTracingHitGroup callableGroup = {
.closestHitShader = collection->callableShader, .closestHitShader = collection->callableShader,
.anyHitShader = anyhit,
}; };
callableGroup.parameters.resize(sizeof(VertexData::DrawCallOffsets)); callableGroup.parameters.resize(sizeof(VertexData::DrawCallOffsets));
std::memcpy(callableGroup.parameters.data(), &transparentData.offsets, sizeof(VertexData::DrawCallOffsets)); std::memcpy(callableGroup.parameters.data(), &transparentData.offsets, sizeof(VertexData::DrawCallOffsets));
@@ -200,14 +202,15 @@ void RayTracingPass::publishOutputs() {
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR); Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
ShaderCompilationInfo compileInfo = { ShaderCompilationInfo compileInfo = {
.name = "RayGenMiss", .name = "RayGenMiss",
.modules = {"RayGen", "Miss"}, .modules = {"RayGen", "AnyHit", "Miss"},
.entryPoints = {{"raygen", "RayGen"}, {"miss", "Miss"}}, .entryPoints = {{"raygen", "RayGen"}, {"anyhit", "AnyHit"}, {"miss", "Miss"}},
.defines = {{"RAY_TRACING", "1"}}, .defines = {{"RAY_TRACING", "1"}},
.rootSignature = pipelineLayout, .rootSignature = pipelineLayout,
}; };
graphics->beginShaderCompilation(compileInfo); graphics->beginShaderCompilation(compileInfo);
rayGen = graphics->createRayGenShader({0}); rayGen = graphics->createRayGenShader({0});
miss = graphics->createMissShader({1}); anyhit = graphics->createAnyHitShader({1});
miss = graphics->createMissShader({2});
pipelineLayout->create(); pipelineLayout->create();
} }
@@ -22,6 +22,7 @@ class RayTracingPass : public RenderPass {
Gfx::PTextureCube skyBox; Gfx::PTextureCube skyBox;
Gfx::OSampler skyBoxSampler; Gfx::OSampler skyBoxSampler;
Gfx::ORayGenShader rayGen; Gfx::ORayGenShader rayGen;
Gfx::OAnyHitShader anyhit;
Gfx::OMissShader miss; Gfx::OMissShader miss;
Gfx::PRayTracingPipeline pipeline; Gfx::PRayTracingPipeline pipeline;
Gfx::OTopLevelAS tlas; Gfx::OTopLevelAS tlas;
+2 -1
View File
@@ -66,7 +66,7 @@ void ShaderCompiler::compile() {
OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout); OPipelineLayout layout = graphics->createPipelineLayout(pass.baseLayout->getName(), pass.baseLayout);
layout->addDescriptorLayout(vd->getVertexDataLayout()); layout->addDescriptorLayout(vd->getVertexDataLayout());
layout->addDescriptorLayout(vd->getInstanceDataLayout()); layout->addDescriptorLayout(vd->getInstanceDataLayout());
permutation.setMaterial(mat->getName()); permutation.setMaterial(mat->getName(), mat->getProfile());
createShaders(permutation, std::move(layout), name); createShaders(permutation, std::move(layout), name);
}); });
} }
@@ -108,6 +108,7 @@ void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipeline
if (std::strlen(permutation.materialName) > 0) { if (std::strlen(permutation.materialName) > 0) {
createInfo.modules.add(permutation.materialName); createInfo.modules.add(permutation.materialName);
createInfo.defines["MATERIAL_FILE_NAME"] = permutation.materialName; createInfo.defines["MATERIAL_FILE_NAME"] = permutation.materialName;
//createInfo.typeParameter.add({"IBRDF", "Phong"});
} }
if (permutation.positionOnly) { if (permutation.positionOnly) {
createInfo.defines["POS_ONLY"] = "1"; createInfo.defines["POS_ONLY"] = "1";
+11 -8
View File
@@ -91,6 +91,7 @@ struct ShaderPermutation {
char vertexMeshFile[32]; char vertexMeshFile[32];
char fragmentFile[32]; char fragmentFile[32];
char vertexDataName[32]; char vertexDataName[32];
char brdfProfile[32];
char materialName[64]; char materialName[64];
uint8 hasFragment; uint8 hasFragment;
uint8 useMeshShading; uint8 useMeshShading;
@@ -106,36 +107,38 @@ struct ShaderPermutation {
void setTaskFile(std::string_view name) { void setTaskFile(std::string_view name) {
std::memset(taskFile, 0, sizeof(taskFile)); std::memset(taskFile, 0, sizeof(taskFile));
hasTaskShader = 1; hasTaskShader = 1;
strncpy(taskFile, name.data(), sizeof(taskFile)); strncpy(taskFile, name.data(), name.size());
} }
void setVertexFile(std::string_view name) { void setVertexFile(std::string_view name) {
std::memset(vertexMeshFile, 0, sizeof(vertexMeshFile)); std::memset(vertexMeshFile, 0, sizeof(vertexMeshFile));
useMeshShading = 0; useMeshShading = 0;
strncpy(vertexMeshFile, name.data(), sizeof(vertexMeshFile)); strncpy(vertexMeshFile, name.data(), name.size());
} }
void setMeshFile(std::string_view name) { void setMeshFile(std::string_view name) {
std::memset(vertexMeshFile, 0, sizeof(vertexMeshFile)); std::memset(vertexMeshFile, 0, sizeof(vertexMeshFile));
useMeshShading = 1; useMeshShading = 1;
strncpy(vertexMeshFile, name.data(), sizeof(vertexMeshFile)); strncpy(vertexMeshFile, name.data(), name.size());
} }
void setRayTracingFile(std::string_view name) { void setRayTracingFile(std::string_view name) {
std::memset(vertexMeshFile, 0, sizeof(vertexMeshFile)); std::memset(vertexMeshFile, 0, sizeof(vertexMeshFile));
rayTracing = true; rayTracing = true;
strncpy(vertexMeshFile, name.data(), sizeof(vertexMeshFile)); strncpy(vertexMeshFile, name.data(), name.size());
} }
void setFragmentFile(std::string_view name) { void setFragmentFile(std::string_view name) {
std::memset(fragmentFile, 0, sizeof(fragmentFile)); std::memset(fragmentFile, 0, sizeof(fragmentFile));
hasFragment = 1; hasFragment = 1;
strncpy(fragmentFile, name.data(), sizeof(fragmentFile)); strncpy(fragmentFile, name.data(), name.size());
} }
void setVertexData(std::string_view name) { void setVertexData(std::string_view name) {
std::memset(vertexDataName, 0, sizeof(vertexDataName)); std::memset(vertexDataName, 0, sizeof(vertexDataName));
strncpy(vertexDataName, name.data(), sizeof(vertexDataName)); strncpy(vertexDataName, name.data(), name.size());
} }
void setMaterial(std::string_view name) { void setMaterial(std::string_view name, std::string_view brdf) {
std::memset(materialName, 0, sizeof(materialName)); std::memset(materialName, 0, sizeof(materialName));
std::memset(brdfProfile, 0, sizeof(brdfProfile));
useMaterial = 1; useMaterial = 1;
strncpy(materialName, name.data(), sizeof(materialName)); strncpy(materialName, name.data(), name.size());
strncpy(brdfProfile, brdf.data(), brdf.size());
} }
void setPositionOnly(bool enable) { positionOnly = enable; } void setPositionOnly(bool enable) { positionOnly = enable; }
void setDepthCulling(bool enable) { depthCulling = enable; } void setDepthCulling(bool enable) { depthCulling = enable; }
@@ -508,6 +508,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
uint32 intersectionIndex = VK_SHADER_UNUSED_KHR; uint32 intersectionIndex = VK_SHADER_UNUSED_KHR;
if (hitgroup.anyHitShader != nullptr) { if (hitgroup.anyHitShader != nullptr) {
auto anyHit = hitgroup.anyHitShader.cast<AnyHitShader>(); auto anyHit = hitgroup.anyHitShader.cast<AnyHitShader>();
anyHitIndex = shaderStages.size();
shaderStages.add(VkPipelineShaderStageCreateInfo{ shaderStages.add(VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr, .pNext = nullptr,
@@ -520,6 +521,7 @@ PRayTracingPipeline PipelineCache::createPipeline(Gfx::RayTracingPipelineCreateI
} }
if (hitgroup.intersectionShader != nullptr) { if (hitgroup.intersectionShader != nullptr) {
auto intersect = hitgroup.intersectionShader.cast<IntersectionShader>(); auto intersect = hitgroup.intersectionShader.cast<IntersectionShader>();
intersectionIndex = shaderGroups.size();
shaderStages.add(VkPipelineShaderStageCreateInfo{ shaderStages.add(VkPipelineShaderStageCreateInfo{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr, .pNext = nullptr,
+5 -4
View File
@@ -18,6 +18,7 @@ using namespace Seele;
{ \ { \
if (diagnostics) { \ if (diagnostics) { \
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl; \ std::cout << (const char*)diagnostics->getBufferPointer() << std::endl; \
abort(); \
} \ } \
} }
@@ -88,7 +89,7 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg
sessionDesc.preprocessorMacroCount = macros.size(); sessionDesc.preprocessorMacroCount = macros.size();
sessionDesc.preprocessorMacros = macros.data(); sessionDesc.preprocessorMacros = macros.data();
slang::TargetDesc targetDesc; slang::TargetDesc targetDesc;
targetDesc.profile = globalSession->findProfile("glsl_450"); targetDesc.profile = globalSession->findProfile("spv_1_4");
targetDesc.format = target; targetDesc.format = target;
sessionDesc.targetCount = 1; sessionDesc.targetCount = 1;
sessionDesc.targets = &targetDesc; sessionDesc.targets = &targetDesc;
@@ -149,10 +150,10 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg
if (info.name == "RayGenMiss") if (info.name == "RayGenMiss")
{ {
layout->addMapping("pVertexData", 1); layout->addMapping("pVertexData", 1);
layout->addMapping("pResources", 4);
layout->addMapping("pLightEnv", 3);
layout->addMapping("pRayTracingParams", 5);
layout->addMapping("pScene", 2); layout->addMapping("pScene", 2);
layout->addMapping("pLightEnv", 3);
layout->addMapping("pResources", 4);
layout->addMapping("pRayTracingParams", 5);
} }
// layout->addMapping("pWaterMaterial", 1); // layout->addMapping("pWaterMaterial", 1);
} }
+1
View File
@@ -172,6 +172,7 @@ void Material::compile() {
for (const auto& [name, exp] : brdf.variables) { for (const auto& [name, exp] : brdf.variables) {
codeStream << "\t\tresult." << name << " = " << varState[exp] << ";" << std::endl; codeStream << "\t\tresult." << name << " = " << varState[exp] << ";" << std::endl;
} }
codeStream << "\t\tresult.transformNormal(input.tangentToWorld);" << std::endl;
codeStream << "\t\treturn result;\n"; codeStream << "\t\treturn result;\n";
codeStream << "\t}\n"; codeStream << "\t}\n";
codeStream << "};\n"; codeStream << "};\n";
+1
View File
@@ -27,6 +27,7 @@ class Material {
OMaterialInstance instantiate(); OMaterialInstance instantiate();
const std::string& getName() const { return materialName; } const std::string& getName() const { return materialName; }
const std::string& getProfile() const { return brdf.profile; }
bool isTwoSided() const; bool isTwoSided() const;
bool hasTransparency() const; bool hasTransparency() const;