Skip to content

Commit 8eaa714

Browse files
committed
fix intersector, no use intersectdata
1 parent cb5662a commit 8eaa714

File tree

6 files changed

+207
-142
lines changed

6 files changed

+207
-142
lines changed

31_HLSLPathTracer/app_resources/hlsl/common.hlsl

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -203,32 +203,32 @@ struct Shape;
203203
template<>
204204
struct Shape<PST_SPHERE>
205205
{
206-
static Shape<PST_SPHERE> create(NBL_CONST_REF_ARG(float32_t3) position, float32_t radius, uint32_t bsdfLightIDs)
206+
static Shape<PST_SPHERE> create(NBL_CONST_REF_ARG(float32_t3) position, float32_t radius2, uint32_t bsdfLightIDs)
207207
{
208208
Shape<PST_SPHERE> retval;
209209
retval.position = position;
210-
retval.radius2 = radius * radius;
210+
retval.radius2 = radius2;
211211
retval.bsdfLightIDs = bsdfLightIDs;
212212
return retval;
213213
}
214214

215215
static Shape<PST_SPHERE> create(NBL_CONST_REF_ARG(float32_t3) position, float32_t radius, uint32_t bsdfID, uint32_t lightID)
216216
{
217217
uint32_t bsdfLightIDs = glsl::bitfieldInsert<uint32_t>(bsdfID, lightID, 16, 16);
218-
return create(position, radius, bsdfLightIDs);
218+
return create(position, radius * radius, bsdfLightIDs);
219219
}
220220

221221
// return intersection distance if found, nan otherwise
222222
float intersect(NBL_CONST_REF_ARG(float32_t3) origin, NBL_CONST_REF_ARG(float32_t3) direction)
223223
{
224224
float32_t3 relOrigin = origin - position;
225-
float relOriginLen2 = nbl::hlsl::dot(relOrigin, relOrigin);
225+
float relOriginLen2 = hlsl::dot<float32_t3>(relOrigin, relOrigin);
226226

227-
float dirDotRelOrigin = nbl::hlsl::dot(direction, relOrigin);
227+
float dirDotRelOrigin = hlsl::dot<float32_t3>(direction, relOrigin);
228228
float det = radius2 - relOriginLen2 + dirDotRelOrigin * dirDotRelOrigin;
229229

230230
// do some speculative math here
231-
float detsqrt = nbl::hlsl::sqrt(det);
231+
float detsqrt = hlsl::sqrt<float32_t>(det);
232232
return -dirDotRelOrigin + (relOriginLen2 > radius2 ? (-detsqrt) : detsqrt);
233233
}
234234

@@ -241,7 +241,7 @@ struct Shape<PST_SPHERE>
241241
float getSolidAngle(NBL_CONST_REF_ARG(float32_t3) origin)
242242
{
243243
float32_t3 dist = position - origin;
244-
float cosThetaMax = nbl::hlsl::sqrt(1.0 - radius2 / nbl::hlsl::dot(dist, dist));
244+
float cosThetaMax = hlsl::sqrt<float32_t>(1.0 - radius2 / hlsl::dot<float32_t3>(dist, dist));
245245
return 2.0 * numbers::pi<float> * (1.0 - cosThetaMax);
246246
}
247247

@@ -255,28 +255,28 @@ struct Shape<PST_SPHERE>
255255
float32_t3 generate_and_pdf(NBL_REF_ARG(float32_t) pdf, NBL_REF_ARG(float32_t) newRayMaxT, NBL_CONST_REF_ARG(float32_t3) origin, NBL_CONST_REF_ARG(Aniso) interaction, bool isBSDF, float32_t3 xi)
256256
{
257257
float32_t3 Z = position - origin;
258-
const float distanceSQ = nbl::hlsl::dot(Z,Z);
258+
const float distanceSQ = hlsl::dot<float32_t3>(Z,Z);
259259
const float cosThetaMax2 = 1.0 - radius2 / distanceSQ;
260260
if (cosThetaMax2 > 0.0)
261261
{
262-
const float rcpDistance = 1.0 / nbl::hlsl::sqrt(distanceSQ);
262+
const float rcpDistance = 1.0 / hlsl::sqrt<float32_t>(distanceSQ);
263263
Z *= rcpDistance;
264264

265-
const float cosThetaMax = nbl::hlsl::sqrt(cosThetaMax2);
265+
const float cosThetaMax = hlsl::sqrt<float32_t>(cosThetaMax2);
266266
const float cosTheta = nbl::hlsl::mix<float>(1.0, cosThetaMax, xi.x);
267267

268268
float32_t3 L = Z * cosTheta;
269269

270270
const float cosTheta2 = cosTheta * cosTheta;
271-
const float sinTheta = nbl::hlsl::sqrt(1.0 - cosTheta2);
271+
const float sinTheta = hlsl::sqrt<float32_t>(1.0 - cosTheta2);
272272
float sinPhi, cosPhi;
273-
math::sincos(2.0 * numbers::pi<float> * xi.y - numbers::pi<float>, sinPhi, cosPhi);
273+
math::sincos<float>(2.0 * numbers::pi<float> * xi.y - numbers::pi<float>, sinPhi, cosPhi);
274274
float32_t3 X, Y;
275275
math::frisvad<float32_t3>(Z, X, Y);
276276

277277
L += (X * cosPhi + Y * sinPhi) * sinTheta;
278278

279-
newRayMaxT = (cosTheta - nbl::hlsl::sqrt(cosTheta2 - cosThetaMax2)) / rcpDistance;
279+
newRayMaxT = (cosTheta - hlsl::sqrt<float32_t>(cosTheta2 - cosThetaMax2)) / rcpDistance;
280280
pdf = 1.0 / (2.0 * numbers::pi<float> * (1.0 - cosThetaMax));
281281
return L;
282282
}
@@ -315,26 +315,26 @@ struct Shape<PST_TRIANGLE>
315315
{
316316
const float32_t3 edges[2] = { vertex1 - vertex0, vertex2 - vertex0 };
317317

318-
const float32_t3 h = nbl::hlsl::cross(direction, edges[1]);
319-
const float a = nbl::hlsl::dot(edges[0], h);
318+
const float32_t3 h = hlsl::cross<float32_t3>(direction, edges[1]);
319+
const float a = hlsl::dot<float32_t3>(edges[0], h);
320320

321321
const float32_t3 relOrigin = origin - vertex0;
322322

323-
const float u = nbl::hlsl::dot(relOrigin, h) / a;
323+
const float u = hlsl::dot<float32_t3>(relOrigin, h) / a;
324324

325-
const float32_t3 q = nbl::hlsl::cross(relOrigin, edges[0]);
326-
const float v = nbl::hlsl::dot(direction, q) / a;
325+
const float32_t3 q = hlsl::cross<float32_t3>(relOrigin, edges[0]);
326+
const float v = hlsl::dot<float32_t3>(direction, q) / a;
327327

328-
const float t = nbl::hlsl::dot(edges[1], q) / a;
328+
const float t = hlsl::dot<float32_t3>(edges[1], q) / a;
329329

330330
const bool intersection = t > 0.f && u >= 0.f && v >= 0.f && (u + v) <= 1.f;
331-
return intersection ? t : numeric_limits<float>::infinity;
331+
return intersection ? t : bit_cast<float, uint32_t>(numeric_limits<float>::infinity);
332332
}
333333

334334
float32_t3 getNormalTimesArea()
335335
{
336336
const float32_t3 edges[2] = { vertex1 - vertex0, vertex2 - vertex0 };
337-
return nbl::hlsl::cross(edges[0], edges[1]) * 0.5f;
337+
return hlsl::cross<float32_t3>(edges[0], edges[1]) * 0.5f;
338338
}
339339

340340
template<typename Ray>
@@ -347,7 +347,7 @@ struct Shape<PST_TRIANGLE>
347347
{
348348
const float dist = ray.intersectionT;
349349
const float32_t3 L = ray.direction;
350-
return dist * dist / nbl::hlsl::abs(nbl::hlsl::dot(getNormalTimesArea(), L));
350+
return dist * dist / hlsl::abs<float32_t>(hlsl::dot<float32_t3>(getNormalTimesArea(), L));
351351
}
352352
break;
353353
case PPM_SOLID_ANGLE:
@@ -381,15 +381,15 @@ struct Shape<PST_TRIANGLE>
381381
{
382382
const float32_t3 edge0 = vertex1 - vertex0;
383383
const float32_t3 edge1 = vertex2 - vertex0;
384-
const float sqrtU = nbl::hlsl::sqrt(xi.x);
384+
const float sqrtU = hlsl::sqrt<float32_t>(xi.x);
385385
float32_t3 pnt = vertex0 + edge0 * (1.0 - sqrtU) + edge1 * sqrtU * xi.y;
386386
float32_t3 L = pnt - origin;
387387

388-
const float distanceSq = nbl::hlsl::dot(L,L);
389-
const float rcpDistance = 1.0 / nbl::hlsl::sqrt(distanceSq);
388+
const float distanceSq = hlsl::dot<float32_t3>(L,L);
389+
const float rcpDistance = 1.0 / hlsl::sqrt<float32_t>(distanceSq);
390390
L *= rcpDistance;
391391

392-
pdf = distanceSq / nbl::hlsl::abs(nbl::hlsl::dot(nbl::hlsl::cross(edge0, edge1) * 0.5f, L));
392+
pdf = distanceSq / hlsl::abs<float32_t>(hlsl::dot<float32_t3>(hlsl::cross<float32_t3>(edge0, edge1) * 0.5f, L));
393393
newRayMaxT = 1.0 / rcpDistance;
394394
return L;
395395
}
@@ -406,7 +406,7 @@ struct Shape<PST_TRIANGLE>
406406
pdf = rcpPdf > numeric_limits<float>::min ? (1.0 / rcpPdf) : 0.0;
407407

408408
const float32_t3 N = getNormalTimesArea();
409-
newRayMaxT = nbl::hlsl::dot(N, vertex0 - origin) / nbl::hlsl::dot(N, L);
409+
newRayMaxT = hlsl::dot<float32_t3>(N, vertex0 - origin) / hlsl::dot<float32_t3>(N, L);
410410
return L;
411411
}
412412
break;
@@ -422,7 +422,7 @@ struct Shape<PST_TRIANGLE>
422422
pdf = rcpPdf > numeric_limits<float>::min ? (1.0 / rcpPdf) : 0.0;
423423

424424
const float32_t3 N = getNormalTimesArea();
425-
newRayMaxT = nbl::hlsl::dot(N, vertex0 - origin) / nbl::hlsl::dot(N, L);
425+
newRayMaxT = hlsl::dot<float32_t3>(N, vertex0 - origin) / hlsl::dot<float32_t3>(N, L);
426426
return L;
427427
}
428428
break;
@@ -462,25 +462,25 @@ struct Shape<PST_RECTANGLE>
462462

463463
float intersect(NBL_CONST_REF_ARG(float32_t3) origin, NBL_CONST_REF_ARG(float32_t3) direction)
464464
{
465-
const float32_t3 h = nbl::hlsl::cross(direction, edge1);
466-
const float a = nbl::hlsl::dot(edge0, h);
465+
const float32_t3 h = hlsl::cross<float32_t3>(direction, edge1);
466+
const float a = hlsl::dot<float32_t3>(edge0, h);
467467

468468
const float32_t3 relOrigin = origin - offset;
469469

470-
const float u = nbl::hlsl::dot(relOrigin,h)/a;
470+
const float u = hlsl::dot<float32_t3>(relOrigin,h)/a;
471471

472-
const float32_t3 q = nbl::hlsl::cross(relOrigin, edge0);
473-
const float v = nbl::hlsl::dot(direction, q) / a;
472+
const float32_t3 q = hlsl::cross<float32_t3>(relOrigin, edge0);
473+
const float v = hlsl::dot<float32_t3>(direction, q) / a;
474474

475-
const float t = nbl::hlsl::dot(edge1, q) / a;
475+
const float t = hlsl::dot<float32_t3>(edge1, q) / a;
476476

477477
const bool intersection = t > 0.f && u >= 0.f && v >= 0.f && u <= 1.f && v <= 1.f;
478-
return intersection ? t : numeric_limits<float>::infinity;
478+
return intersection ? t : bit_cast<float, uint32_t>(numeric_limits<float>::infinity);
479479
}
480480

481481
float32_t3 getNormalTimesArea()
482482
{
483-
return nbl::hlsl::cross(edge0, edge1);
483+
return hlsl::cross<float32_t3>(edge0, edge1);
484484
}
485485

486486
void getNormalBasis(NBL_REF_ARG(float32_t3x3) basis, NBL_REF_ARG(float32_t2) extents)
@@ -502,7 +502,7 @@ struct Shape<PST_RECTANGLE>
502502
{
503503
const float dist = ray.intersectionT;
504504
const float32_t3 L = ray.direction;
505-
return dist * dist / nbl::hlsl::abs(nbl::hlsl::dot(getNormalTimesArea(), L));
505+
return dist * dist / hlsl::abs<float32_t>(hlsl::dot<float32_t3>(getNormalTimesArea(), L));
506506
}
507507
break;
508508
// #ifdef TRIANGLE_REFERENCE ?
@@ -542,10 +542,10 @@ struct Shape<PST_RECTANGLE>
542542
case PPM_AREA:
543543
{
544544
float32_t3 L = origin2origin + edge0 * xi.x + edge1 * xi.y;
545-
const float distSq = nbl::hlsl::dot(L, L);
546-
const float rcpDist = 1.0 / nbl::hlsl::sqrt(distSq);
545+
const float distSq = hlsl::dot<float32_t3>(L, L);
546+
const float rcpDist = 1.0 / hlsl::sqrt<float32_t>(distSq);
547547
L *= rcpDist;
548-
pdf = distSq / nbl::hlsl::abs(nbl::hlsl::dot(N, L));
548+
pdf = distSq / hlsl::abs<float32_t>(hlsl::dot<float32_t3>(N, L));
549549
newRayMaxT = 1.0 / rcpDist;
550550
return L;
551551
}
@@ -572,7 +572,7 @@ struct Shape<PST_RECTANGLE>
572572
else
573573
pdf = numeric_limits<float>::infinity;
574574

575-
newRayMaxT = nbl::hlsl::dot(N, origin2origin) / nbl::hlsl::dot(N, L);
575+
newRayMaxT = hlsl::dot<float32_t3>(N, origin2origin) / hlsl::dot<float32_t3>(N, L);
576576
return L;
577577
}
578578
break;

31_HLSLPathTracer/app_resources/hlsl/intersector.hlsl

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,62 @@ struct Comprehensive
2525
using bxdfnode_type = BxdfNode;
2626
using scene_type = Scene<light_type, bxdfnode_type>;
2727

28+
static ObjectID traceRay(NBL_REF_ARG(ray_type) ray, NBL_CONST_REF_ARG(scene_type) scene)
29+
{
30+
ObjectID objectID;
31+
objectID.id = -1;
32+
33+
// prodedural shapes
34+
for (int i = 0; i < scene.sphereCount; i++)
35+
{
36+
float t = scene.spheres[i].intersect(ray.origin, ray.direction);
37+
38+
bool closerIntersection = t > 0.0 && t < ray.intersectionT;
39+
40+
if (closerIntersection)
41+
{
42+
ray.intersectionT = t;
43+
objectID.id = i;
44+
objectID.mode = IntersectData::Mode::PROCEDURAL;
45+
objectID.shapeType = PST_SPHERE;
46+
}
47+
}
48+
for (int i = 0; i < scene.triangleCount; i++)
49+
{
50+
float t = scene.triangles[i].intersect(ray.origin, ray.direction);
51+
52+
bool closerIntersection = t > 0.0 && t < ray.intersectionT;
53+
54+
if (closerIntersection)
55+
{
56+
ray.intersectionT = t;
57+
objectID.id = i;
58+
objectID.mode = IntersectData::Mode::PROCEDURAL;
59+
objectID.shapeType = PST_TRIANGLE;
60+
}
61+
}
62+
for (int i = 0; i < scene.rectangleCount; i++)
63+
{
64+
float t = scene.rectangles[i].intersect(ray.origin, ray.direction);
65+
66+
bool closerIntersection = t > 0.0 && t < ray.intersectionT;
67+
68+
if (closerIntersection)
69+
{
70+
ray.intersectionT = t;
71+
objectID.id = i;
72+
objectID.mode = IntersectData::Mode::PROCEDURAL;
73+
objectID.shapeType = PST_TRIANGLE;
74+
}
75+
}
76+
77+
// TODO: trace AS
78+
79+
return objectID;
80+
}
81+
82+
// note for future consideration: still need to encode to IntersectData?
83+
// obsolete?
2884
static ObjectID traceProcedural(NBL_REF_ARG(ray_type) ray, NBL_CONST_REF_ARG(IntersectData) intersect)
2985
{
3086
const bool anyHit = ray.intersectionT != numeric_limits<scalar_type>::max;
@@ -81,6 +137,7 @@ struct Comprehensive
81137
return objectID;
82138
}
83139

140+
// obsolete?
84141
static ObjectID traceRay(NBL_REF_ARG(ray_type) ray, NBL_CONST_REF_ARG(IntersectData) intersect)
85142
{
86143
const IntersectData::Mode mode = (IntersectData::Mode)intersect.mode;
@@ -109,36 +166,36 @@ struct Comprehensive
109166
return ObjectID::create(-1, 0, PST_SPHERE);
110167
}
111168

112-
static ObjectID traceRay(NBL_REF_ARG(ray_type) ray, NBL_CONST_REF_ARG(scene_type) scene)
113-
{
114-
IntersectData data;
169+
// static ObjectID traceRay(NBL_REF_ARG(ray_type) ray, NBL_CONST_REF_ARG(scene_type) scene)
170+
// {
171+
// IntersectData data;
115172

116-
ObjectID objectID;
117-
objectID.id = -1; // start with no intersect
173+
// ObjectID objectID;
174+
// objectID.id = -1; // start with no intersect
118175

119-
// prodedural shapes
120-
if (scene.sphereCount > 0)
121-
{
122-
data = scene.toIntersectData(ext::Intersector::IntersectData::Mode::PROCEDURAL, PST_SPHERE);
123-
objectID = traceRay(ray, data);
124-
}
176+
// // prodedural shapes
177+
// if (scene.sphereCount > 0)
178+
// {
179+
// data = scene.toIntersectData(ext::Intersector::IntersectData::Mode::PROCEDURAL, PST_SPHERE);
180+
// objectID = traceRay(ray, data);
181+
// }
125182

126-
if (scene.triangleCount > 0)
127-
{
128-
data = scene.toIntersectData(ext::Intersector::IntersectData::Mode::PROCEDURAL, PST_TRIANGLE);
129-
objectID = traceRay(ray, data);
130-
}
183+
// if (scene.triangleCount > 0)
184+
// {
185+
// data = scene.toIntersectData(ext::Intersector::IntersectData::Mode::PROCEDURAL, PST_TRIANGLE);
186+
// objectID = traceRay(ray, data);
187+
// }
131188

132-
if (scene.rectangleCount > 0)
133-
{
134-
data = scene.toIntersectData(ext::Intersector::IntersectData::Mode::PROCEDURAL, PST_RECTANGLE);
135-
objectID = traceRay(ray, data);
136-
}
189+
// if (scene.rectangleCount > 0)
190+
// {
191+
// data = scene.toIntersectData(ext::Intersector::IntersectData::Mode::PROCEDURAL, PST_RECTANGLE);
192+
// objectID = traceRay(ray, data);
193+
// }
137194

138-
// TODO: trace AS
195+
// // TODO: trace AS
139196

140-
return objectID;
141-
}
197+
// return objectID;
198+
// }
142199
};
143200

144201
// does everything in traceray in ex 30

0 commit comments

Comments
 (0)