Permalink
Browse files

Don't copy matrices and vectors in to Matrix function args

brings first 6 passes down from 28s to 23s on westmere chip, bottleneck
changes from mrMul to main:

 31.27%  gloss  gloss                 [.] main
 27.95%  gloss  gloss                 [.] mrMul
 19.61%  gloss  gloss                 [.] sceneObjectBoxIntersectRay
 14.29%  gloss  gloss                 [.] sceneObjectUnitPlaneIntersectRay
  • Loading branch information...
giannitedesco committed Aug 23, 2013
1 parent cc4d199 commit 689dbfcc46ba1005d3db667bd1b43f6b23be9926
Showing with 67 additions and 47 deletions.
  1. +17 −16 Matrix.c
  2. +11 −10 Matrix.h
  3. +11 −3 Scene.c
  4. +5 −4 SceneObjectBox.c
  5. +5 −4 SceneObjectPlane.c
  6. +13 −6 SceneObjectSphere.c
  7. +5 −4 SceneObjectUnitPlane.c
View
@@ -72,58 +72,59 @@ bool mEqual(const Matrix a, const Matrix b) {
return true;
}
-__attribute__((const))
-Matrix mMul(const Matrix a, const Matrix b) {
+__attribute__((pure))
+Matrix mMul(const Matrix *a, const Matrix *b) {
Matrix matrix = makeMatrixZero();
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
for (int k=0; k<4; k++)
- matrix.values[i][j] += a.values[i][k] * b.values[k][j];
+ matrix.values[i][j] += a->values[i][k] * b->values[k][j];
return matrix;
}
-__attribute__((const))
-Vector mvMul(const Matrix matrix, const Vector vector) {
+__attribute__((pure))
+Vector mvMul(const Matrix *matrix, const Vector *vector) {
Vector newVector = mvMulDir(matrix, vector);
// Access x,y,z as an array.
float* newVectorValues = (float*)&newVector.x;
for (int i=0; i<3; i++)
- newVectorValues[i] += matrix.values[i][3];
+ newVectorValues[i] += matrix->values[i][3];
return newVector;
}
-__attribute__((const))
-Vector mvMulDir(const Matrix matrix, const Vector vector) {
+__attribute__((pure))
+Vector mvMulDir(const Matrix *matrix, const Vector *vector) {
Vector newVector = makeVector(0, 0, 0);
// Access x,y,z as an array.
- float* vectorValues = (float*)&vector.x;
+ float* vectorValues = (float*)&vector->x;
float* newVectorValues = (float*)&newVector.x;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
- newVectorValues[i] += matrix.values[i][j] * vectorValues[j];
+ newVectorValues[i] += matrix->values[i][j] * vectorValues[j];
return newVector;
}
-__attribute__((const))
-Ray mrMul(const Matrix matrix, const Ray ray) {
+__attribute__((pure))
+Ray mrMul(const Matrix *matrix, const Ray *ray) {
- return makeRay(mvMul(matrix, ray.origin), vNormalized(mvMulDir(matrix, ray.direction)));
+ return makeRay(mvMul(matrix, &ray->origin),
+ vNormalized(mvMulDir(matrix, &ray->direction)));
}
-__attribute__((const))
-Matrix mInversed(const Matrix matrix) {
- float* m = (float*)&matrix.values[0][0];
+__attribute__((pure))
+Matrix mInversed(const Matrix *matrix) {
+ float* m = (float*)&matrix->values[0][0];
Matrix returnValue;
float* out = &returnValue.values[0][0];
View
@@ -20,18 +20,19 @@ Matrix makeMatrixAxisAngle(const Vector axis, const float angle);
__attribute__((const))
bool mEqual(const Matrix a, const Matrix b);
-__attribute__((const))
-Matrix mMul(const Matrix a, const Matrix b);
+__attribute__((pure))
+Matrix mMul(const Matrix *a, const Matrix *b);
-__attribute__((const))
-Vector mvMul (const Matrix Matrix, const Vector Vector);
-__attribute__((const))
-Vector mvMulDir(const Matrix Matrix, const Vector Vector);
+__attribute__((pure))
+Vector mvMul (const Matrix *Matrix, const Vector *Vector);
-__attribute__((const))
-Ray mrMul(const Matrix matrix, const Ray ray);
+__attribute__((pure))
+Vector mvMulDir(const Matrix *Matrix, const Vector *Vector);
-__attribute__((const))
-Matrix mInversed(const Matrix);
+__attribute__((pure))
+Ray mrMul(const Matrix *matrix, const Ray *ray);
+
+__attribute__((pure))
+Matrix mInversed(const Matrix *);
#endif // MATRIX_H
View
14 Scene.c
@@ -28,8 +28,10 @@ Color sceneTraceRayAtPixel(const Scene *scene, const int currentPixel, const int
float maxX = tanf(cameraFov/360.0*PI);
float x = ((((currentPixel % width) + randf()) / width ) * 2 - 1) * maxX;
float y = -((((currentPixel / width) + randf()) / height) * 2 - 1) * maxX / cameraAspectRatio;
+ Matrix m = mInversed(&scene->cameraOrientation);
+ Ray r = makeRay(makeVectorOrigo(), vNormalized(makeVector(x, y, 1)));
- return sceneTraceRay(scene, mrMul(mInversed(scene->cameraOrientation), makeRay(makeVectorOrigo(), vNormalized(makeVector(x, y, 1)))), numCameraRayBounces);
+ return sceneTraceRay(scene, mrMul(&m, &r), numCameraRayBounces);
}
void sceneGeneratePhotons(Scene *scene, const int lightRayBounces, const int numPhotonsPerLightSource) {
@@ -187,14 +189,20 @@ void buildCornellBox(Scene *scene) {
// Boxes
+ Matrix a, b;
+ a = makeMatrixAxisAngle(makeVector(0, 1, 0), .3);
+ b = makeMatrixTranslation(makeVector(-.3, -.3, .3));
sceneObjectContainerAddValue(&scene->objects, makeSceneObjectBox(
makeVector(.3, .7, .3),
- mMul(makeMatrixAxisAngle(makeVector(0, 1, 0), .3), makeMatrixTranslation(makeVector(-.3, -.3, .3))),
+ mMul(&a, &b),
whiteMaterial
));
+
+ a = makeMatrixAxisAngle(makeVector(0, 1, 0), -.3);
+ b = makeMatrixTranslation(makeVector(.3, -.75, -.3));
sceneObjectContainerAddValue(&scene->objects, makeSceneObjectBox(
makeVector(.3, .3, .3),
- mMul(makeMatrixAxisAngle(makeVector(0, 1, 0), -.3), makeMatrixTranslation(makeVector(.3, -.75, -.3))),
+ mMul(&a, &b),
whiteMaterial
));
View
@@ -9,17 +9,18 @@ const SceneObjectVTable sceneObjectBoxVTable = {
SceneObject makeSceneObjectBox (const Vector size, const Matrix transform, const Material *material) {
- return (SceneObject) {&sceneObjectBoxVTable, material, transform, mInversed(transform), {.box = makeBox(size)}};
+ return (SceneObject) {&sceneObjectBoxVTable, material, transform, mInversed(&transform), {.box = makeBox(size)}};
}
Intersection sceneObjectBoxIntersectRay(const SceneObject object, const Ray ray) {
- Intersection intersection = bIntersect(object.box, mrMul(object.inversedTransform, ray));
+ Intersection intersection = bIntersect(object.box,
+ mrMul(&object.inversedTransform, &ray));
if (intersection.hitType) {
- intersection.normal = mvMulDir(object.transform, intersection.normal );
- intersection.position = mvMul (object.transform, intersection.position);
+ intersection.normal = mvMulDir(&object.transform, &intersection.normal );
+ intersection.position = mvMul (&object.transform, &intersection.position);
intersection.material = object.material;
if (intersection.material->isPerfectBlack) {
View
@@ -7,17 +7,18 @@ const SceneObjectVTable sceneObjectPlaneVTable = {
SceneObject makeSceneObjectPlane (const Plane plane, const Matrix transform, const Material *material) {
- return (SceneObject) {&sceneObjectPlaneVTable, material, transform, mInversed(transform), {.plane = plane}};
+ return (SceneObject) {&sceneObjectPlaneVTable, material, transform, mInversed(&transform), {.plane = plane}};
}
Intersection sceneObjectPlaneIntersectRay(const SceneObject object, const Ray ray) {
- Intersection intersection = pIntersect(object.plane, mrMul(object.inversedTransform, ray));
+ Intersection intersection = pIntersect(object.plane,
+ mrMul(&object.inversedTransform, &ray));
if (intersection.hitType) {
- intersection.normal = mvMulDir(object.transform, intersection.normal );
- intersection.position = mvMul (object.transform, intersection.position);
+ intersection.normal = mvMulDir(&object.transform, &intersection.normal );
+ intersection.position = mvMul (&object.transform, &intersection.position);
intersection.material = object.material;
if (intersection.material->isPerfectBlack) {
View
@@ -11,17 +11,18 @@ const SceneObjectVTable sceneObjectSphereVTable = {
SceneObject makeSceneObjectSphere (const Sphere sphere, const Matrix transform, const Material *material) {
- return (SceneObject) {&sceneObjectSphereVTable, material, transform, mInversed(transform), {.sphere = sphere}};
+ return (SceneObject) {&sceneObjectSphereVTable, material, transform, mInversed(&transform), {.sphere = sphere}};
}
Intersection sceneObjectSphereIntersectRay(const SceneObject object, const Ray ray) {
- Intersection intersection = sIntersect(object.sphere, mrMul(object.inversedTransform, ray));
+ Intersection intersection = sIntersect(object.sphere,
+ mrMul(&object.inversedTransform, &ray));
if (intersection.hitType) {
- intersection.normal = mvMulDir(object.transform, intersection.normal );
- intersection.position = mvMul (object.transform, intersection.position);
+ intersection.normal = mvMulDir(&object.transform, &intersection.normal );
+ intersection.position = mvMul (&object.transform, &intersection.position);
intersection.material = object.material;
if (intersection.material->isPerfectBlack) {
@@ -91,7 +92,10 @@ bool sceneObjectSphereEmitPhotons(const SceneObject object, const int numPhotons
Vector position = vAdd(object.sphere.position, vsMul(normal, 1+vEpsilon));
- photonContainerAddValue(photons, makePhoton(mrMul(object.transform, makeRay(position, normal)), csMul(object.material->radience, 1.0 / numPhotons)));
+ Ray r = makeRay(position, normal);
+ photonContainerAddValue(photons,
+ makePhoton(mrMul(&object.transform, &r),
+ csMul(object.material->radience, 1.0 / numPhotons)));
}
}
@@ -108,7 +112,10 @@ bool sceneObjectSphereEmitPhotons(const SceneObject object, const int numPhotons
Vector position = vAdd(object.sphere.position, vsMul(normal, 1+vEpsilon));
- photonContainerAddValue(photons, makePhoton(mrMul(object.transform, makeRay(position, normal)), csMul(object.material->radience, 1.0 / numPhotons)));
+ Ray r = makeRay(position, normal);
+ photonContainerAddValue(photons,
+ makePhoton(mrMul(&object.transform, &r),
+ csMul(object.material->radience, 1.0 / numPhotons)));
}
return true;
View
@@ -9,12 +9,13 @@ const SceneObjectVTable sceneObjectUnitPlaneVTable = {
SceneObject makeSceneObjectUnitPlane (const Plane plane, const Matrix transform, const Material *material) {
- return (SceneObject) {&sceneObjectUnitPlaneVTable, material, transform, mInversed(transform), {.plane = plane}};
+ return (SceneObject) {&sceneObjectUnitPlaneVTable, material, transform, mInversed(&transform), {.plane = plane}};
}
Intersection sceneObjectUnitPlaneIntersectRay(const SceneObject object, const Ray ray) {
- Intersection intersection = pIntersect(object.plane, mrMul(object.inversedTransform, ray));
+ Intersection intersection = pIntersect(object.plane,
+ mrMul(&object.inversedTransform, &ray));
if (intersection.hitType) {
@@ -31,8 +32,8 @@ Intersection sceneObjectUnitPlaneIntersectRay(const SceneObject object, const Ra
return intersection;
}
- intersection.normal = mvMulDir(object.transform, intersection.normal );
- intersection.position = mvMul (object.transform, intersection.position);
+ intersection.normal = mvMulDir(&object.transform, &intersection.normal );
+ intersection.position = mvMul (&object.transform, &intersection.position);
intersection.material = object.material;
if (intersection.material->isPerfectBlack) {

1 comment on commit 689dbfc

@giannitedesco

This comment has been minimized.

Show comment
Hide comment
@giannitedesco

giannitedesco Aug 23, 2013

Owner

Pointers, the whole point of C :)

Owner

giannitedesco commented on 689dbfc Aug 23, 2013

Pointers, the whole point of C :)

Please sign in to comment.