Skip to content

Commit

Permalink
Implements geometry union
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninputer committed Apr 9, 2012
1 parent 2ac6e47 commit 70e2507
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 167 deletions.
1 change: 0 additions & 1 deletion AMPDemo/MandelbrotViewer/MandelbrotViewer.vcxproj
Expand Up @@ -98,7 +98,6 @@
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="mandelbrot.cpp" />
<ClCompile Include="MandelbrotViewer.cpp" />
<ClCompile Include="MandelbrotViewerApplication.cpp" />
<ClCompile Include="RenderArea.cpp" />
Expand Down
3 changes: 0 additions & 3 deletions AMPDemo/MandelbrotViewer/MandelbrotViewer.vcxproj.filters
Expand Up @@ -44,9 +44,6 @@
<ClCompile Include="MandelbrotViewerApplication.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="mandelbrot.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RenderArea.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
41 changes: 0 additions & 41 deletions AMPDemo/MandelbrotViewer/mandelbrot.cpp

This file was deleted.

39 changes: 38 additions & 1 deletion AMPDemo/MandelbrotViewer/mandelbrot.h
@@ -1,7 +1,44 @@
#include "amp.h"
#include "amp_math.h"

unsigned int set_hsb (float hue, float saturate, float bright) restrict (amp);
inline unsigned int set_hsb (float hue, float saturate, float bright) restrict (amp)
{

float red, green, blue;
float h = (hue * 256) / 60;
float p = bright * (1 - saturate);
float q = bright * (1 - saturate * (h - (int)h));
float t = bright * (1 - saturate * (1 - (h - (int)h)));

switch ((int)h) {
case 0:
red = bright, green = t, blue = p;
break;
case 1:
red = q, green = bright, blue = p;
break;
case 2:
red = p, green = bright, blue = t;
break;
case 3:
red = p, green = q, blue = bright;
break;
case 4:
red = t, green = p, blue = bright;
break;
case 5:
case 6:
red = bright, green = p, blue = q;
break;
}

unsigned int ired, igreen, iblue;
ired = (unsigned int)(red * 255.0f);
igreen = (unsigned int)(green * 255.0f);
iblue = (unsigned int)(blue * 255.0f);

return 0xff000000 | (ired << 16) | (igreen << 8) | iblue;
}

template<typename fp_t>
void generate_mandelbrot(
Expand Down
1 change: 1 addition & 0 deletions ampdemo/RayTracing/RayTracing.vcxproj
Expand Up @@ -96,6 +96,7 @@
<ClInclude Include="RayTracingApplication.h" />
<ClInclude Include="RenderArea.h" />
<ClInclude Include="Resource.h" />
<ClInclude Include="geometry.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="ampvectors.h" />
Expand Down
83 changes: 26 additions & 57 deletions ampdemo/RayTracing/ampmathhelper.h
Expand Up @@ -4,65 +4,34 @@
#include <amp_math.h>
#include <math.h>

template<typename fp_t>
class math_helper_cpu
namespace gpu
{
public:
static fp_t sqrt(fp_t x) restrict(cpu) { return ::sqrt(x); }
static fp_t rsqrt(fp_t x) restrict(cpu) { return 1 / ::sqrt(x); }
static fp_t tan(fp_t x) restrict(cpu) { return ::tan(x); }
static fp_t fabs(fp_t x) restrict(cpu) { return ::fabs(x); }
static fp_t floor(fp_t x) restrict(cpu) { return ::floor(x); }
static fp_t fmin(fp_t x, fp_t y) restrict(cpu) { return std::min(x, y); }
static fp_t fmax(fp_t x, fp_t y) restrict(cpu) { return std::max(x, y); }
static fp_t pow(fp_t x, fp_t y) restrict(cpu) { return ::pow(x, y); }
};

template<typename fp_t>
class math_helper;
template<typename fp_t> inline fp_t sqrt(fp_t x) restrict(cpu) { return ::sqrt(x); }
template<typename fp_t> inline fp_t rsqrt(fp_t x) restrict(cpu) { return 1 / ::sqrt(x); }
template<typename fp_t> inline fp_t tan(fp_t x) restrict(cpu) { return ::tan(x); }
template<typename fp_t> inline fp_t fabs(fp_t x) restrict(cpu) { return ::fabs(x); }
template<typename fp_t> inline fp_t floor(fp_t x) restrict(cpu) { return ::floor(x); }
template<typename fp_t> inline fp_t fmin(fp_t x, fp_t y) restrict(cpu) { return std::min(x, y); }
template<typename fp_t> inline fp_t fmax(fp_t x, fp_t y) restrict(cpu) { return std::max(x, y); }
template<typename fp_t> inline fp_t pow(fp_t x, fp_t y) restrict(cpu) { return ::pow(x, y); }

template<>
class math_helper<float> : public math_helper_cpu<float>
{
public:
using math_helper_cpu<float>::sqrt;
using math_helper_cpu<float>::rsqrt;
using math_helper_cpu<float>::tan;
using math_helper_cpu<float>::fabs;
using math_helper_cpu<float>::floor;
using math_helper_cpu<float>::fmin;
using math_helper_cpu<float>::fmax;
using math_helper_cpu<float>::pow;

static float sqrt(float x) restrict(amp) { return Concurrency::fast_math::sqrt(x); }
static float rsqrt(float x) restrict(amp) { return Concurrency::fast_math::rsqrt(x); }
static float tan(float x) restrict(amp) { return Concurrency::fast_math::tan(x); }
static float fabs(float x) restrict(amp) { return Concurrency::fast_math::fabs(x); }
static float floor(float x) restrict(amp) { return Concurrency::fast_math::floor(x); }
static float fmin(float x, float y) restrict(amp) { return Concurrency::fast_math::fmin(x, y); }
static float fmax(float x, float y) restrict(amp) { return Concurrency::fast_math::fmax(x, y); }
static float pow(float x, float y) restrict(amp) { return Concurrency::fast_math::pow(x, y); }
};
inline float sqrt(float x) restrict(amp) { return Concurrency::fast_math::sqrt(x); }
inline float rsqrt(float x) restrict(amp) { return Concurrency::fast_math::rsqrt(x); }
inline float tan(float x) restrict(amp) { return Concurrency::fast_math::tan(x); }
inline float fabs(float x) restrict(amp) { return Concurrency::fast_math::fabs(x); }
inline float floor(float x) restrict(amp) { return Concurrency::fast_math::floor(x); }
inline float fmin(float x, float y) restrict(amp) { return Concurrency::fast_math::fmin(x, y); }
inline float fmax(float x, float y) restrict(amp) { return Concurrency::fast_math::fmax(x, y); }
inline float pow(float x, float y) restrict(amp) { return Concurrency::fast_math::pow(x, y); }

template<>
class math_helper<double> : public math_helper_cpu<double>
{
public:
using math_helper_cpu<double>::sqrt;
using math_helper_cpu<double>::rsqrt;
using math_helper_cpu<double>::tan;
using math_helper_cpu<double>::fabs;
using math_helper_cpu<double>::floor;
using math_helper_cpu<double>::fmin;
using math_helper_cpu<double>::fmax;
using math_helper_cpu<double>::pow;
inline double sqrt(double x) restrict(amp) { return Concurrency::precise_math::sqrt(x); }
inline double rsqrt(double x) restrict(amp) { return Concurrency::precise_math::rsqrt(x); }
inline double tan(double x) restrict(amp) { return Concurrency::precise_math::tan(x); }
inline double fabs(double x) restrict(amp) { return Concurrency::precise_math::fabs(x); }
inline double floor(double x) restrict(amp) { return Concurrency::precise_math::floor(x); }
inline double fmin(double x, double y) restrict(amp) { return Concurrency::precise_math::fmin(x, y); }
inline double fmax(double x, double y) restrict(amp) { return Concurrency::precise_math::fmax(x, y); }
inline double pow(double x, double y) restrict(amp) { return Concurrency::precise_math::pow(x, y); }

static double sqrt(double x) restrict(amp) { return Concurrency::precise_math::sqrt(x); }
static double rsqrt(double x) restrict(amp) { return Concurrency::precise_math::rsqrt(x); }
static double tan(double x) restrict(amp) { return Concurrency::precise_math::tan(x); }
static double fabs(double x) restrict(amp) { return Concurrency::precise_math::fabs(x); }
static double floor(double x) restrict(amp) { return Concurrency::precise_math::floor(x); }
static double fmin(double x, double y) restrict(amp) { return Concurrency::precise_math::fmin(x, y); }
static double fmax(double x, double y) restrict(amp) { return Concurrency::precise_math::fmax(x, y); }
static double pow(double x, double y) restrict(amp) { return Concurrency::precise_math::pow(x, y); }
};
}
4 changes: 2 additions & 2 deletions ampdemo/RayTracing/ampvectors.h
Expand Up @@ -23,12 +23,12 @@ class vector3

fp_t length() const restrict(cpu, amp)
{
return math_helper<fp_t>::sqrt(sqr_length());
return gpu::sqrt(sqr_length());
}

vector3 normalize() const restrict(cpu, amp)
{
fp_t inv = math_helper<fp_t>::rsqrt(sqr_length());
fp_t inv = gpu::rsqrt(sqr_length());

return vector3(inv * x, inv * y, inv * z);
}
Expand Down
108 changes: 108 additions & 0 deletions ampdemo/RayTracing/geometry.h
@@ -0,0 +1,108 @@
#pragma once

#include <amp.h>
#include <amp_math.h>
#include "raycommon.h"

class geometry
{
public:
enum geometry_type
{
geometry_none,
geometry_sphere,
geometry_plane
};

template <typename fp_t>
intersect_result<fp_t> intersect(const ray<fp_t>& ray) const restrict(amp)
{
switch (type)
{
case geometry_sphere:
return static_cast<const sphere<fp_t>*>(this)->intersect_impl(ray);
case geometry_plane:
return static_cast<const plane<fp_t>*>(this)->intersect_impl(ray);
default:
return intersect_result<fp_t>();
}
}
protected:
geometry(int type, int material) restrict(cpu, amp) : material(material), type(type) {}
int material;
private:
int type;
};

template <typename fp_t>
class sphere : public geometry
{
public:
vector3<fp_t> center;
fp_t radius;

explicit sphere(const vector3<fp_t>& center, fp_t radius, int material) restrict(cpu, amp) : geometry(geometry_sphere, material), center(center), radius(radius) { init(); }

intersect_result<fp_t> intersect_impl(const ray<fp_t>& ray) const restrict(amp)
{
vector3<fp_t> v = ray.origin - center;
fp_t a0 = v.sqr_length() - sqr_radius;
fp_t d_dot_v = ray.direction.dot(v);

if (d_dot_v <= 0 )
{
fp_t discr = d_dot_v * d_dot_v - a0;
fp_t distance = -d_dot_v - gpu::sqrt(discr);
vector3<fp_t> position(ray.get_point(distance));

if (discr >= 0)
{
return intersect_result<fp_t>(
true,
material,
distance,
position,
(position - center).normalize()
);

}
}

return intersect_result<fp_t>();
}

private:
fp_t sqr_radius;
void init()
{
sqr_radius = radius * radius;
}
};

template <typename fp_t>
class plane : public geometry
{
public:
vector3<fp_t> normal;
fp_t d;

explicit plane(const vector3<fp_t>& normal, fp_t d, int material) restrict(cpu, amp) : geometry(geometry_plane, material), normal(normal), d(d) { init(); }

intersect_result<fp_t> intersect_impl(const ray<fp_t>& ray) const restrict(amp)
{
fp_t a = ray.direction.dot(normal);

if (a >= 0) return intersect_result<fp_t>();

fp_t b = normal.dot(ray.origin - position);
fp_t distance = -b / a;

return intersect_result<fp_t>(true, material, distance, ray.get_point(distance), normal);
}
private:
vector3<fp_t> position;
void init()
{
position = normal * d;
}
};

0 comments on commit 70e2507

Please sign in to comment.