Skip to content

Commit 63b64e3

Browse files
committed
made scene a static global var
1 parent 011fbfb commit 63b64e3

File tree

2 files changed

+91
-46
lines changed

2 files changed

+91
-46
lines changed

31_HLSLPathTracer/app_resources/hlsl/render.comp.hlsl

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#include "nbl/builtin/hlsl/bxdf/reflection.hlsl"
77
#include "nbl/builtin/hlsl/bxdf/transmission.hlsl"
88

9-
#include "render_common.hlsl"
10-
#include "pathtracer.hlsl"
11-
129
// add these defines (one at a time) using -D argument to dxc
1310
// #define SPHERE_LIGHT
1411
// #define TRIANGLE_LIGHT
@@ -17,10 +14,33 @@
1714
#ifdef SPHERE_LIGHT
1815
#define SPHERE_COUNT 9
1916
#define LIGHT_TYPE ext::PST_SPHERE
20-
#else
17+
18+
#define TRIANGLE_COUNT 0
19+
#define RECTANGLE_COUNT 0
20+
#endif
21+
22+
#ifdef TRIANGLE_LIGHT
23+
#define TRIANGLE_COUNT 1
24+
#define LIGHT_TYPE ext::PST_TRIANGLE
25+
26+
#define SPHERE_COUNT 8
27+
#define RECTANGLE_COUNT 0
28+
#endif
29+
30+
#ifdef RECTANGLE_LIGHT
31+
#define RECTANGLE_COUNT 1
32+
#define LIGHT_TYPE ext::PST_RECTANGLE
33+
2134
#define SPHERE_COUNT 8
35+
#define TRIANGLE_COUNT 0
2236
#endif
2337

38+
#define LIGHT_COUNT 1
39+
#define BXDF_COUNT 7
40+
41+
#include "render_common.hlsl"
42+
#include "pathtracer.hlsl"
43+
2444
using namespace nbl::hlsl;
2545

2646
NBL_CONSTEXPR uint32_t WorkgroupSize = 32;
@@ -80,22 +100,21 @@ static const ext::Shape<ext::PST_SPHERE> spheres[SPHERE_COUNT] = {
80100
};
81101

82102
#ifdef TRIANGLE_LIGHT
83-
#define LIGHT_TYPE ext::PST_TRIANGLE
84-
#define TRIANGLE_COUNT 1
85103
static const ext::Shape<ext::PST_TRIANGLE> triangles[TRIANGLE_COUNT] = {
86104
ext::Shape<ext::PST_TRIANGLE>::create(float3(-1.8,0.35,0.3) * 10.0, float3(-1.2,0.35,0.0) * 10.0, float3(-1.5,0.8,-0.3) * 10.0, bxdfnode_type::INVALID_ID, 0u)
87105
};
106+
#else
107+
static const ext::Shape<ext::PST_TRIANGLE> triangles[1];
88108
#endif
89109

90110
#ifdef RECTANGLE_LIGHT
91-
#define LIGHT_TYPE ext::PST_RECTANGLE
92-
#define RECTANGLE_COUNT 1
93111
static const ext::Shape<ext::PST_RECTANGLE> rectangles[RECTANGLE_COUNT] = {
94112
ext::Shape<ext::PST_RECTANGLE>::create(float3(-3.8,0.35,1.3), normalize(float3(2,0,-1))*7.0, normalize(float3(2,-5,4))*0.1, bxdfnode_type::INVALID_ID, 0u)
95113
};
114+
#else
115+
static const ext::Shape<ext::PST_RECTANGLE> rectangles[1];
96116
#endif
97117

98-
#define LIGHT_COUNT 1
99118
static const light_type lights[LIGHT_COUNT] = {
100119
light_type::create(spectral_t(30.0,25.0,15.0),
101120
#ifdef SPHERE_LIGHT
@@ -106,7 +125,6 @@ static const light_type lights[LIGHT_COUNT] = {
106125
ext::IntersectMode::IM_PROCEDURAL, LIGHT_TYPE)
107126
};
108127

109-
#define BXDF_COUNT 7
110128
static const bxdfnode_type bxdfs[BXDF_COUNT] = {
111129
bxdfnode_type::create(ext::MaterialSystem::MaterialType::DIFFUSE, false, float2(0,0), spectral_t(0.8,0.8,0.8)),
112130
bxdfnode_type::create(ext::MaterialSystem::MaterialType::DIFFUSE, false, float2(0,0), spectral_t(0.8,0.4,0.4)),
@@ -117,6 +135,12 @@ static const bxdfnode_type bxdfs[BXDF_COUNT] = {
117135
bxdfnode_type::create(ext::MaterialSystem::MaterialType::DIELECTRIC, false, float2(0.0625,0.0625), spectral_t(1,1,1), spectral_t(0.71,0.69,0.67))
118136
};
119137

138+
static const ext::Scene<light_type, bxdfnode_type> scene = ext::Scene<light_type, bxdfnode_type>::create(
139+
spheres, triangles, rectangles,
140+
SPHERE_COUNT, TRIANGLE_COUNT, RECTANGLE_COUNT,
141+
lights, LIGHT_COUNT, bxdfs, BXDF_COUNT
142+
);
143+
120144
[numthreads(WorkgroupSize, WorkgroupSize, 1)]
121145
void main(uint32_t3 threadID : SV_DispatchThreadID)
122146
{
@@ -164,32 +188,6 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
164188

165189
pathtracer_type pathtracer = pathtracer_type::create(ptCreateParams);
166190

167-
// set up scene (can do as global var?)
168-
ext::Scene<light_type, bxdfnode_type> scene;
169-
scene.sphereCount = SPHERE_COUNT;
170-
for (uint32_t i = 0; i < SPHERE_COUNT; i++)
171-
scene.spheres[i] = spheres[i];
172-
#ifdef TRIANGLE_LIGHT
173-
scene.triangleCount = TRIANGLE_COUNT;
174-
for (uint32_t i = 0; i < TRIANGLE_COUNT; i++)
175-
scene.triangles[i] = triangles[i];
176-
#else
177-
scene.triangleCount = 0;
178-
#endif
179-
#ifdef RECTANGLE_LIGHT
180-
scene.rectangleCount = RECTANGLE_COUNT;
181-
for (uint32_t i = 0; i < RECTANGLE_COUNT; i++)
182-
scene.rectangles[i] = rectangles[i];
183-
#else
184-
scene.rectangleCount = 0;
185-
#endif
186-
scene.lightCount = LIGHT_COUNT;
187-
for (uint32_t i = 0; i < LIGHT_COUNT; i++)
188-
scene.lights[i] = lights[i];
189-
scene.bxdfCount = BXDF_COUNT;
190-
for (uint32_t i = 0; i < BXDF_COUNT; i++)
191-
scene.bxdfs[i] = bxdfs[i];
192-
193191
float32_t3 color = pathtracer.getMeasure(pc.sampleCount, pc.depth, scene);
194192
float32_t4 pixCol = float32_t4(color, 1.0);
195193
outImage[coords] = pixCol;

31_HLSLPathTracer/app_resources/hlsl/scene.hlsl

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,78 @@ struct Scene
1515
{
1616
using light_type = Light;
1717
using bxdfnode_type = BxdfNode;
18+
using this_t = Scene<Light, BxdfNode>;
1819

19-
NBL_CONSTEXPR_STATIC_INLINE uint32_t maxSphereCount = 25;
20-
NBL_CONSTEXPR_STATIC_INLINE uint32_t maxTriangleCount = 12;
21-
NBL_CONSTEXPR_STATIC_INLINE uint32_t maxRectangleCount = 12;
20+
// NBL_CONSTEXPR_STATIC_INLINE uint32_t maxSphereCount = 25;
21+
// NBL_CONSTEXPR_STATIC_INLINE uint32_t maxTriangleCount = 12;
22+
// NBL_CONSTEXPR_STATIC_INLINE uint32_t maxRectangleCount = 12;
2223

23-
Shape<PST_SPHERE> spheres[maxSphereCount];
24-
Shape<PST_TRIANGLE> triangles[maxTriangleCount];
25-
Shape<PST_RECTANGLE> rectangles[maxRectangleCount];
24+
#if SPHERE_COUNT < 1
25+
#define SCENE_SPHERE_COUNT 1
26+
#else
27+
#define SCENE_SPHERE_COUNT SPHERE_COUNT
28+
#endif
29+
30+
#if TRIANGLE_COUNT < 1
31+
#define SCENE_TRIANGLE_COUNT 1
32+
#else
33+
#define SCENE_TRIANGLE_COUNT TRIANGLE_COUNT
34+
#endif
35+
36+
#if RECTANGLE_COUNT < 1
37+
#define SCENE_RECTANGLE_COUNT 1
38+
#else
39+
#define SCENE_RECTANGLE_COUNT RECTANGLE_COUNT
40+
#endif
41+
42+
Shape<PST_SPHERE> spheres[SCENE_SPHERE_COUNT];
43+
Shape<PST_TRIANGLE> triangles[SCENE_TRIANGLE_COUNT];
44+
Shape<PST_RECTANGLE> rectangles[SCENE_RECTANGLE_COUNT];
2645

2746
uint32_t sphereCount;
2847
uint32_t triangleCount;
2948
uint32_t rectangleCount;
3049

31-
NBL_CONSTEXPR_STATIC_INLINE uint32_t maxLightCount = 4;
50+
// NBL_CONSTEXPR_STATIC_INLINE uint32_t maxLightCount = 4;
3251

33-
light_type lights[maxLightCount];
52+
light_type lights[LIGHT_COUNT];
3453
uint32_t lightCount;
3554

36-
NBL_CONSTEXPR_STATIC_INLINE uint32_t maxBxdfCount = 16; // TODO: limit change?
55+
// NBL_CONSTEXPR_STATIC_INLINE uint32_t maxBxdfCount = 16;
3756

38-
bxdfnode_type bxdfs[maxBxdfCount];
57+
bxdfnode_type bxdfs[BXDF_COUNT];
3958
uint32_t bxdfCount;
4059

4160
// AS ases;
4261

62+
static this_t create(
63+
NBL_CONST_REF_ARG(Shape<PST_SPHERE>) spheres[SCENE_SPHERE_COUNT],
64+
NBL_CONST_REF_ARG(Shape<PST_TRIANGLE>) triangles[SCENE_TRIANGLE_COUNT],
65+
NBL_CONST_REF_ARG(Shape<PST_RECTANGLE>) rectangles[SCENE_RECTANGLE_COUNT],
66+
uint32_t sphereCount, uint32_t triangleCount, uint32_t rectangleCount,
67+
NBL_CONST_REF_ARG(light_type) lights[LIGHT_COUNT], uint32_t lightCount,
68+
NBL_CONST_REF_ARG(bxdfnode_type) bxdfs[BXDF_COUNT], uint32_t bxdfCount)
69+
{
70+
this_t retval;
71+
retval.spheres = spheres;
72+
retval.triangles = triangles;
73+
retval.rectangles = rectangles;
74+
retval.sphereCount = sphereCount;
75+
retval.triangleCount = triangleCount;
76+
retval.rectangleCount = rectangleCount;
77+
78+
retval.lights = lights;
79+
retval.lightCount = lightCount;
80+
81+
retval.bxdfs = bxdfs;
82+
retval.bxdfCount = bxdfCount;
83+
return retval;
84+
}
85+
86+
#undef SCENE_SPHERE_COUNT
87+
#undef SCENE_TRIANGLE_COUNT
88+
#undef SCENE_RECTANGLE_COUNT
89+
4390
// obsolete?
4491
// Intersector::IntersectData toIntersectData(uint32_t mode, ProceduralShapeType type)
4592
// {

0 commit comments

Comments
 (0)