rt backface culling

This commit is contained in:
Dynamitos
2025-01-29 21:55:11 +01:00
parent 09660ab642
commit 44b147084b
7 changed files with 65 additions and 25 deletions
+34
View File
@@ -0,0 +1,34 @@
import RayTracingData;
import Scene;
import VertexData;
import MaterialParameter;
import StaticMeshVertexData;
[shader("anyhit")]
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);
InstanceData inst = pScene.instances[InstanceID()];
MeshData m = pScene.meshData[InstanceID()];
// offset into the index buffer
uint indexOffset = m.firstIndex;
// added to indices to reference correct part of global mesh pool
uint vertexOffset = pScene.meshletInfos[m.meshletOffset].indicesOffset;
uint vertexIndex0 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 0];
uint vertexIndex1 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 1];
uint vertexIndex2 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 2];
VertexAttributes attr0 = pVertexData.getAttributes(vertexIndex0);
VertexAttributes attr1 = pVertexData.getAttributes(vertexIndex1);
VertexAttributes attr2 = pVertexData.getAttributes(vertexIndex2);
FragmentParameter f0 = attr0.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
FragmentParameter f1 = attr1.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
FragmentParameter f2 = attr2.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
}
+24 -20
View File
@@ -71,7 +71,11 @@ import MATERIAL_FILE_NAME;
*/
static const float eps = 1e-5;
const static float ka = 0;
const static float ks = 0;
const static float3 fogEmm = float3(0, 0.01, 0.01);
const static float eps = 1e-5;
[shader("closesthit")]
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
{
@@ -116,15 +120,15 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
float3 rnd = rand01(hitValue.rndSeed);
//float kt = ka + ks;
//float s = -log(rnd.z) / kt;
//float3 xs = r.o + s * r.d;
//if (s < t) {
//if (s < RayTCurrent()) {
// float3 xs = WorldRayOrigin() + s * WorldRayDirection();
// float p = kt * rnd.z;
// if (depth > 5) {
// if (rnd.z >= p) break;
// else accmat /= p;
// if (hitValue.depth > 5) {
// if (rnd.z >= p) return;
// else brdf.baseColor /= p;
// }
// float3 ldirect = nextEventEstimation(accmat, r.d, xs, -r.d, kt, true, rnd);
// accrad += (fogEmm + ks * ldirect) / kt;
// float3 ldirect = nextEventEstimation(brdf.baseColor, WorldRayDirection(), xs, -WorldRayDirection(), kt, true, rnd);
// hitValue.light += (fogEmm + ks * ldirect) / kt;
// accmat *= ks / kt;
// rayDesc.Origin = xs;
// rayDesc.Direction = float3(
@@ -132,14 +136,13 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
// sin(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y),
// rnd.y
// );
// continue;
// TraceRay(scene, 0, 0xff, 0, 0, rayDesc);
//}
//float p = max(max(brdf.baseColor.x, brdf.baseColor.y), brdf.baseColor.z);
//if(hitValue.depth > 5) {
// if (rnd.z >= p) return;
// else hitValue.accmat /= p;
//}
float p = max(max(brdf.baseColor.x, brdf.baseColor.y), brdf.baseColor.z);
if(hitValue.depth > 5) {
if (rnd.z >= p) return;
}
hitValue.light += brdf.emissive * hitValue.emissive + brdf.evaluateAmbient();
//-- Ideal DIFFUSE reflection
@@ -160,12 +163,12 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
payload.anyHit = true;
payload.hit = false;
payload.rndSeed = hitValue.rndSeed;
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xff, 0, 0, 0, rayDesc, payload);
// we have missed all geometry, so directional light is affecting us
//if(!payload.hit) {
//hitValue.light += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
//}
if(!payload.hit) {
hitValue.light += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
}
}
for(uint i = 0; i < pLightEnv.numPointLights; ++i) {
RayPayload payload;
@@ -180,7 +183,7 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
rayDesc.TMin = eps;
rayDesc.Origin = x;
rayDesc.Direction = l;
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xff, 0, 0, 0, rayDesc, payload);
// hitting only after the light
if(!payload.hit) {
@@ -206,8 +209,9 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
payload.depth = hitValue.depth;
payload.anyHit = false;
payload.rndSeed = hitValue.rndSeed + 1;
TraceRay(pRayTracingParams.scene, 0, 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);
hitValue.light += brdf.evaluate(params.getTangentToWorld(), -WorldRayDirection(), rayDesc.Direction, payload.light / bias);
}
hitValue.light /= p;
}
+1 -4
View File
@@ -11,9 +11,6 @@ struct Ray
const static float S_O = 6.9;
const static float f = 0.035;
const static float A = 0.0;
const static float ka = 0;
const static float ks = 0;
const static float3 fogEmm = float3(0, 0.01, 0.01);
struct SampleParams
{
@@ -110,7 +107,7 @@ void raygen()
payload.depth = 1;
payload.rndSeed = rndSeed + 1;
payload.anyHit = false;
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xff, 0, 0, 0, rayDesc, payload);
if(pSamps.pass == 0) pRayTracingParams.radianceAccumulator[pix] = float4(0);
float3 accumulatedRadiance = payload.light / pSamps.samplesPerPixel;
+1
View File
@@ -38,6 +38,7 @@ int main(int argc, char** argv) {
graphics->init(initializer);
StaticMeshVertexData* vd = StaticMeshVertexData::getInstance();
vd->init(graphics);
getGlobals().useRayTracing = true;
OWindowManager windowManager = new WindowManager();
AssetRegistry::init("Assets", graphics);
+3 -1
View File
@@ -1,6 +1,6 @@
#include "PlayView.h"
#include "Window/Window.h"
#include "Component/Mesh.h"
#include "Window/Window.h"
using namespace Seele;
using namespace Seele::Editor;
@@ -49,4 +49,6 @@ void PlayView::keyCallback(KeyCode code, InputAction action, KeyModifier modifie
if (code == KeyCode::KEY_R && action == InputAction::RELEASE) {
getGlobals().useRayTracing = !getGlobals().useRayTracing;
}
//if (code == KeyCode::KEY_B && action == InputAction::RELEASE && modifier & KeyModifier::MOD_CONTROL) {
//}
}
@@ -1,4 +1,5 @@
#include "GameInterface.h"
#include "Windows.h"
using namespace Seele;
@@ -1,5 +1,6 @@
#pragma once
#include "Game.h"
#define NOIME
#include "Windows.h"
namespace Seele {