Skip to content

Commit 5230ca1

Browse files
committed
Clean up shading
1 parent 34c0d6f commit 5230ca1

File tree

11 files changed

+263
-306
lines changed

11 files changed

+263
-306
lines changed

App/CL/bxdf.cl

Lines changed: 109 additions & 252 deletions
Large diffs are not rendered by default.

App/CL/integrator_pt.cl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ __kernel void ShadeSurface(
363363

364364
// Fill surface data
365365
DifferentialGeometry diffgeo;
366-
FillDifferentialGeometry(&scene, &isect, &diffgeo);
366+
DifferentialGeometry_Fill(&scene, &isect, &diffgeo);
367367

368368
// Check if we are hitting from the inside
369369

@@ -444,13 +444,15 @@ __kernel void ShadeSurface(
444444
// maintain proper volume stack here
445445
//if (Bxdf_IsBtdf(&diffgeo))
446446
//{
447-
// If we entering set the volume
448-
//path->volume = ndotwi > 0.f ? 0 : -1;
447+
// // If we entering set the volume
448+
// path->volume = !backfacing ? 0 : -1;
449449
//}
450450

451451
// Check if we need to apply normal map
452452
//ApplyNormalMap(&diffgeo, TEXTURE_ARGS);
453-
ApplyBumpMap(&diffgeo, TEXTURE_ARGS);
453+
DifferentialGeometry_ApplyBumpMap(&diffgeo, TEXTURE_ARGS);
454+
DifferentialGeometry_CalculateTangentTransforms(&diffgeo);
455+
454456
float lightpdf = 0.f;
455457
float bxdflightpdf = 0.f;
456458
float bxdfpdf = 0.f;

App/CL/normalmap.cl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,33 @@ THE SOFTWARE.
2626
#include <../App/CL/texture.cl>
2727
#include <../App/CL/payload.cl>
2828

29-
void ApplyNormalMap(DifferentialGeometry* dg, TEXTURE_ARG_LIST)
29+
void DifferentialGeometry_ApplyNormalMap(DifferentialGeometry* diffgeo, TEXTURE_ARG_LIST)
3030
{
31-
int nmapidx = dg->mat.nmapidx;
31+
int nmapidx = diffgeo->mat.nmapidx;
3232
if (nmapidx != -1)
3333
{
3434
// Now n, dpdu, dpdv is orthonormal basis
35-
float3 mappednormal = 2.f * Texture_Sample2D(dg->uv, TEXTURE_ARGS_IDX(nmapidx)).xyz - make_float3(1.f, 1.f, 1.f);
35+
float3 mappednormal = 2.f * Texture_Sample2D(diffgeo->uv, TEXTURE_ARGS_IDX(nmapidx)).xyz - make_float3(1.f, 1.f, 1.f);
3636

3737
// Return mapped version
38-
dg->n = normalize(mappednormal.z * dg->n * 0.5f + mappednormal.x * dg->dpdu + mappednormal.y * dg->dpdv);
38+
diffgeo->n = normalize(mappednormal.z * diffgeo->n * 0.5f + mappednormal.x * diffgeo->dpdu + mappednormal.y * diffgeo->dpdv);
39+
diffgeo->dpdv = normalize(cross(diffgeo->n, diffgeo->dpdu));
40+
diffgeo->dpdu = normalize(cross(diffgeo->dpdv, diffgeo->n));
3941
}
4042
}
4143

42-
void ApplyBumpMap(DifferentialGeometry* dg, TEXTURE_ARG_LIST)
44+
void DifferentialGeometry_ApplyBumpMap(DifferentialGeometry* diffgeo, TEXTURE_ARG_LIST)
4345
{
44-
int nmapidx = dg->mat.nmapidx;
46+
int nmapidx = diffgeo->mat.nmapidx;
4547
if (nmapidx != -1)
4648
{
4749
// Now n, dpdu, dpdv is orthonormal basis
48-
float3 mappednormal = 2.f * Texture_SampleBump(dg->uv, TEXTURE_ARGS_IDX(nmapidx)) - make_float3(1.f, 1.f, 1.f);
50+
float3 mappednormal = 2.f * Texture_SampleBump(diffgeo->uv, TEXTURE_ARGS_IDX(nmapidx)) - make_float3(1.f, 1.f, 1.f);
4951

5052
// Return mapped version
51-
dg->n = normalize(mappednormal.z * dg->n + mappednormal.x * dg->dpdu + mappednormal.y * dg->dpdv);
53+
diffgeo->n = normalize(mappednormal.z * diffgeo->n * 0.5f + mappednormal.x * diffgeo->dpdu + mappednormal.y * diffgeo->dpdv);
54+
diffgeo->dpdv = normalize(cross(diffgeo->n, diffgeo->dpdu));
55+
diffgeo->dpdu = normalize(cross(diffgeo->dpdv, diffgeo->n));
5256
}
5357
}
5458

App/CL/payload.cl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ enum Bxdf
7474
kLambert,
7575
kIdealReflect,
7676
kIdealRefract,
77-
kMicrofacetBlinn,
7877
kMicrofacetBeckmann,
7978
kMicrofacetGGX,
8079
kLayered,
@@ -236,6 +235,22 @@ typedef
236235
int extra;
237236
} Texture;
238237

238+
typedef struct __matrix
239+
{
240+
union
241+
{
242+
struct
243+
{
244+
float4 m0;
245+
float4 m1;
246+
float4 m2;
247+
float4 m3;
248+
};
249+
250+
float m[16];
251+
};
252+
} matrix;
253+
239254
// Hit data
240255
typedef struct _DifferentialGeometry
241256
{
@@ -251,6 +266,10 @@ typedef struct _DifferentialGeometry
251266
float3 dpdu;
252267
float3 dpdv;
253268
float area;
269+
270+
matrix world_to_tangent;
271+
matrix tangent_to_world;
272+
254273
// Material
255274
Material mat;
256275
} DifferentialGeometry;

App/CL/scene.cl

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ typedef struct _Scene
5252
} Scene;
5353

5454
/// Fill DifferentialGeometry structure based on intersection info from RadeonRays
55-
void FillDifferentialGeometry(// Scene
55+
void DifferentialGeometry_Fill(// Scene
5656
Scene const* scene,
5757
// RadeonRays intersection
5858
Intersection const* isect,
@@ -107,7 +107,6 @@ void FillDifferentialGeometry(// Scene
107107
diffgeo->mat = scene->materials[matidx];
108108

109109
/// From PBRT book
110-
/// Construct tangent basis on the fly and apply normal map
111110
float du1 = uv0.x - uv2.x;
112111
float du2 = uv1.x - uv2.x;
113112
float dv1 = uv0.y - uv2.y;
@@ -118,33 +117,50 @@ void FillDifferentialGeometry(// Scene
118117

119118
float det = du1 * dv2 - dv1 * du2;
120119

121-
if (det != 0.f)
120+
if (0 && det != 0.f)
122121
{
123122
float invdet = 1.f / det;
124123
diffgeo->dpdu = normalize( (dv2 * dp1 - dv1 * dp2) * invdet );
125124
diffgeo->dpdv = normalize( (-du2 * dp1 + du1 * dp2) * invdet );
126125
}
127126
else
128127
{
129-
diffgeo->dpdu = normalize(GetOrthoVector(diffgeo->ng));
130-
diffgeo->dpdv = normalize(cross(diffgeo->ng, diffgeo->dpdu));
128+
diffgeo->dpdu = normalize(GetOrthoVector(diffgeo->n));
129+
diffgeo->dpdv = normalize(cross(diffgeo->n, diffgeo->dpdu));
131130
}
131+
132132

133133

134134
// Fix all to be orthogonal
135-
diffgeo->dpdv = normalize(cross(diffgeo->n, diffgeo->dpdu));
136-
diffgeo->dpdu = normalize(cross(diffgeo->dpdv, diffgeo->n));
135+
//diffgeo->dpdv = normalize(cross(diffgeo->ng, diffgeo->dpdu));
136+
//diffgeo->dpdu = normalize(cross(diffgeo->dpdv, diffgeo->ng));
137137

138138
float3 p0 = transform_point(v0, shape.m0, shape.m1, shape.m2, shape.m3);
139139
float3 p1 = transform_point(v1, shape.m0, shape.m1, shape.m2, shape.m3);
140140
float3 p2 = transform_point(v2, shape.m0, shape.m1, shape.m2, shape.m3);
141141

142142
diffgeo->area = 0.5f * length(cross(p2 - p0, p2 - p1));
143-
144-
// Apply transform & linear motion blur
145-
//v += (linearvelocity * time);
146-
// MT^-1 should be used if scale is present
147-
//n = rotate_vector(n, angularvelocity);
143+
}
144+
145+
void DifferentialGeometry_CalculateTangentTransforms(DifferentialGeometry* diffgeo)
146+
{
147+
diffgeo->world_to_tangent = matrix_from_rows3(
148+
diffgeo->dpdu,
149+
diffgeo->n,
150+
diffgeo->dpdv);
151+
152+
diffgeo->world_to_tangent.m0.w = -dot(diffgeo->dpdu, diffgeo->p);
153+
diffgeo->world_to_tangent.m1.w = -dot(diffgeo->n, diffgeo->p);
154+
diffgeo->world_to_tangent.m2.w = -dot(diffgeo->dpdv, diffgeo->p);
155+
156+
diffgeo->tangent_to_world = matrix_from_cols3(
157+
diffgeo->world_to_tangent.m0.xyz,
158+
diffgeo->world_to_tangent.m1.xyz,
159+
diffgeo->world_to_tangent.m2.xyz);
160+
161+
diffgeo->tangent_to_world.m0.w = diffgeo->p.x;
162+
diffgeo->tangent_to_world.m1.w = diffgeo->p.y;
163+
diffgeo->tangent_to_world.m2.w = diffgeo->p.z;
148164
}
149165

150166
int Scene_SampleLight(Scene const* scene, float sample, float* pdf)

App/CL/utils.cl

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ THE SOFTWARE.
2424

2525
#define PI 3.14159265358979323846f
2626

27-
// 2D distribution function
28-
typedef struct __Distribution2D
29-
{
30-
int w;
31-
int h;
32-
__global float const* data;
33-
} Distribution2D;
27+
#include <../App/CL/payload.cl>
3428

3529
#ifndef APPLE
3630
/// These functions are defined on OSX already
@@ -70,6 +64,87 @@ int2 make_int2(int x, int y)
7064
}
7165
#endif
7266

67+
float matrix_get(matrix m, int i, int j)
68+
{
69+
return m.m[i * 4 + j];
70+
}
71+
72+
matrix matrix_from_cols(float4 c0, float4 c1, float4 c2, float4 c3)
73+
{
74+
matrix m;
75+
m.m0 = make_float4(c0.x, c1.x, c2.x, c3.x);
76+
m.m1 = make_float4(c0.y, c1.y, c2.y, c3.y);
77+
m.m2 = make_float4(c0.z, c1.z, c2.z, c3.z);
78+
m.m3 = make_float4(c0.w, c1.w, c2.w, c3.w);
79+
return m;
80+
}
81+
82+
matrix matrix_from_rows(float4 c0, float4 c1, float4 c2, float4 c3)
83+
{
84+
matrix m;
85+
m.m0 = c0;
86+
m.m1 = c1;
87+
m.m2 = c2;
88+
m.m3 = c3;
89+
return m;
90+
}
91+
92+
matrix matrix_from_rows3(float3 c0, float3 c1, float3 c2)
93+
{
94+
matrix m;
95+
m.m0.xyz = c0; m.m0.w = 0;
96+
m.m1.xyz = c1; m.m1.w = 0;
97+
m.m2.xyz = c2; m.m2.w = 0;
98+
m.m3 = make_float4(0.f, 0.f, 0.f, 1.f);
99+
return m;
100+
}
101+
102+
matrix matrix_from_cols3(float3 c0, float3 c1, float3 c2)
103+
{
104+
matrix m;
105+
m.m0 = make_float4(c0.x, c1.x, c2.x, 0.f);
106+
m.m1 = make_float4(c0.y, c1.y, c2.y, 0.f);
107+
m.m2 = make_float4(c0.z, c1.z, c2.z, 0.f);
108+
m.m3 = make_float4(0.f, 0.f, 0.f, 1.f);
109+
return m;
110+
}
111+
112+
matrix matrix_transpose(matrix m)
113+
{
114+
return matrix_from_cols(m.m0, m.m1, m.m2, m.m3);
115+
}
116+
117+
float4 matrix_mul_vector4(matrix m, float4 v)
118+
{
119+
float4 res;
120+
res.x = dot(m.m0, v);
121+
res.y = dot(m.m1, v);
122+
res.z = dot(m.m2, v);
123+
res.w = dot(m.m3, v);
124+
return res;
125+
}
126+
127+
float3 matrix_mul_vector3(matrix m, float3 v)
128+
{
129+
float3 res;
130+
res.x = dot(m.m0.xyz, v);
131+
res.y = dot(m.m1.xyz, v);
132+
res.z = dot(m.m2.xyz, v);
133+
return res;
134+
}
135+
136+
float3 matrix_mul_point3(matrix m, float3 v)
137+
{
138+
float3 res;
139+
res.x = dot(m.m0.xyz, v) + m.m0.w;
140+
res.y = dot(m.m1.xyz, v) + m.m1.w;
141+
res.z = dot(m.m2.xyz, v) + m.m2.w;
142+
return res;
143+
}
144+
145+
146+
147+
73148

74149
/// Transform point with transformation matrix.
75150
/// m0...m3 are matrix rows
@@ -169,16 +244,6 @@ float3 GetOrthoVector(float3 n)
169244
return normalize(p);
170245
}
171246

172-
float2 Distribution2D_Sample(Distribution2D const* dist, float2 sample, float* pdf)
173-
{
174-
return make_float2(0.f, 0.f);
175-
}
176-
177-
float Distribution2D_GetPdf(Distribution2D const* dist, float2 sample)
178-
{
179-
return 0.f;
180-
}
181-
182247
uint upper_power_of_two(uint v)
183248
{
184249
v--;

App/Scene/IO/material_io.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ namespace Baikal
8585
return "ideal_reflect";
8686
case Baikal::SingleBxdf::BxdfType::kIdealRefract:
8787
return "ideal_refract";;
88-
case Baikal::SingleBxdf::BxdfType::kMicrofacetBlinn:
89-
return "microfacet_blinn";
9088
case Baikal::SingleBxdf::BxdfType::kMicrofacetBeckmann:
9189
return "microfacet_beckmann";
9290
case Baikal::SingleBxdf::BxdfType::kMicrofacetGGX:
@@ -114,7 +112,6 @@ namespace Baikal
114112
{ "lambert" , Baikal::SingleBxdf::BxdfType::kLambert },
115113
{ "ideal_reflect" , Baikal::SingleBxdf::BxdfType::kIdealReflect },
116114
{ "ideal_refract" , Baikal::SingleBxdf::BxdfType::kIdealRefract },
117-
{ "microfacet_blinn" , Baikal::SingleBxdf::BxdfType::kMicrofacetBlinn },
118115
{ "microfacet_beckmann" , Baikal::SingleBxdf::BxdfType::kMicrofacetBeckmann },
119116
{ "microfacet_ggx" , Baikal::SingleBxdf::BxdfType::kMicrofacetGGX },
120117
{ "emissive" , Baikal::SingleBxdf::BxdfType::kEmissive },
@@ -218,7 +215,6 @@ namespace Baikal
218215

219216
if (type == SingleBxdf::BxdfType::kMicrofacetGGX ||
220217
type == SingleBxdf::BxdfType::kMicrofacetBeckmann ||
221-
type == SingleBxdf::BxdfType::kMicrofacetBlinn ||
222218
type == SingleBxdf::BxdfType::kMicrofacetRefractionGGX ||
223219
type == SingleBxdf::BxdfType::kMicrofacetRefractionBeckmann)
224220
{

App/Scene/IO/scene_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace Baikal
5757
{
5858
auto s = RadeonRays::float3(mat.specular[0], mat.specular[1], mat.specular[2]);
5959

60-
if (s.sqnorm() > 0 || !mat.specular_texname.empty())
60+
if ((s.sqnorm() > 0 || !mat.specular_texname.empty()))
6161
{
6262
// Otherwise create lambert
6363
material = new MultiBxdf(MultiBxdf::Type::kFresnelBlend);

App/Scene/material.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ namespace Baikal
241241
RegisterInput("normal", "Normal map", {InputType::kTexture});
242242
RegisterInput("ior", "Index of refraction", {InputType::kFloat4});
243243
RegisterInput("fresnel", "Fresnel flag", {InputType::kFloat4});
244-
RegisterInput("roughness", "Roughness", {InputType::kFloat4});
244+
RegisterInput("roughness", "Roughness", {InputType::kFloat4, InputType::kTexture});
245245

246246
SetInputValue("albedo", RadeonRays::float4(0.7f, 0.7f, 0.7f, 1.f));
247+
SetInputValue("normal", static_cast<Texture const*>(nullptr));
247248
}
248249

249250
SingleBxdf::BxdfType SingleBxdf::GetBxdfType() const
@@ -268,7 +269,7 @@ namespace Baikal
268269
RegisterInput("base_material", "Base material", {InputType::kMaterial});
269270
RegisterInput("top_material", "Top material", {InputType::kMaterial});
270271
RegisterInput("ior", "Index of refraction", {InputType::kFloat4});
271-
RegisterInput("weight", "Blend weight", {InputType::kFloat4});
272+
RegisterInput("weight", "Blend weight", {InputType::kFloat4, InputType::kTexture});
272273
}
273274

274275
MultiBxdf::Type MultiBxdf::GetType() const

App/Scene/material.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ namespace Baikal
154154
kLambert,
155155
kIdealReflect,
156156
kIdealRefract,
157-
kMicrofacetBlinn,
158157
kMicrofacetBeckmann,
159158
kMicrofacetGGX,
160159
kEmissive,

0 commit comments

Comments
 (0)