1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.2.1 (2010/09/08)
#include "GpuRootFinder.h"
WM5_WINDOW_APPLICATION(GpuRootFinder);
//----------------------------------------------------------------------------
GpuRootFinder::GpuRootFinder ()
:
WindowApplication3("SampleMathematics/GpuRootFinder", 0, 0, 64, 64,
Float4(0.5f, 0.5f, 0.5f, 1.0f)),
mTextColor(0.0f, 0.0f, 0.0f, 1.0f)
{
Environment::InsertDirectory(ThePath + "Shaders/");
mResults = 0;
}
//----------------------------------------------------------------------------
bool GpuRootFinder::OnInitialize ()
{
if (!WindowApplication3::OnInitialize())
{
return false;
}
// Create a screen-space camera to use with the render target.
mScreenCamera = ScreenTarget::CreateCamera();
mRenderer->SetCamera(mScreenCamera);
// Create a screen polygon to use with the render target.
VertexFormat* vformat = VertexFormat::Create(2,
VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);
mScreenPolygon = ScreenTarget::CreateRectangle(vformat, 4096, 2048,
0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
// Create the render targets.
Texture::Format tformat = Texture::TF_A8R8G8B8;
mRenderTarget = new0 RenderTarget(1, tformat, 4096, 2048, false, false);
// Create the results texture.
mResults = new0 Texture2D(Texture::TF_A8R8G8B8, 4096, 2048, 1);
// Create the root-finder effect.
RootFinderEffect* effect = new0 RootFinderEffect();
mScreenPolygon->SetEffectInstance(effect->CreateInstance());
// Compute the roots.
if (mRenderer->PreDraw())
{
int64_t start = GetTimeInMicroseconds();
mRenderer->Enable(mRenderTarget);
mRenderer->Draw(mScreenPolygon);
mRenderer->Disable(mRenderTarget);
mRenderer->ReadColor(0, mRenderTarget, mResults);
unsigned char* data = (unsigned char*)mResults->GetData(0);
const int jmax = 4096*2048;
for (int j = 0; j < jmax; ++j, data += 4)
{
int negMin = data[0];
int posMax = data[1];
int posMin = data[2];
int negMax = data[3];
int e, unbiased;
float unit0, unit1, x0, x1, y0, y1;
if (posMin <= posMax)
{
for (e = posMin; e <= posMax; ++e)
{
unbiased = e - 127;
unit0 = 1.0f + ldexp((float)j, -23);
unit1 = 1.0f + ldexp((float)(j+1), -23);
x0 = ldexp(unit0, unbiased);
x1 = ldexp(unit1, unbiased);
y0 = MyFunction(x0);
y1 = MyFunction(x1);
assertion(y0*y1 <= 0.0f, "Unexpected condition.\n");
mRoots.insert(fabs(y0) <= fabs(y1) ? x0 : x1);
}
}
if (negMin <= negMax)
{
for (e = negMin; e <= negMax; ++e)
{
unbiased = e - 127;
unit0 = 1.0f + ldexp((float)j, -23);
unit1 = 1.0f + ldexp((float)(j+1), -23);
x0 = -ldexp(unit0, unbiased);
x1 = -ldexp(unit1, unbiased);
y0 = MyFunction(x0);
y1 = MyFunction(x1);
assertion(y0*y1 <= 0.0f, "Unexpected condition.\n");
mRoots.insert(fabs(y0) <= fabs(y1) ? x0 : x1);
}
}
}
int64_t final = GetTimeInMicroseconds();
FILE* output = fopen("results.txt", "wt");
fprintf(output, "microseconds = %d\n", (int)(final-start));
std::set<float>::iterator iter = mRoots.begin();
std::set<float>::iterator end = mRoots.end();
for (/**/; iter != end; ++iter)
{
fprintf(output, "root = %f\n", *iter);
}
fclose(output);
mRenderer->PostDraw();
}
// Do not launch the application window.
return false;
}
//----------------------------------------------------------------------------
void GpuRootFinder::OnTerminate ()
{
mScreenCamera = 0;
mRenderTarget = 0;
mScreenPolygon = 0;
delete0(mResults);
WindowApplication3::OnTerminate();
}
//----------------------------------------------------------------------------
float GpuRootFinder::MyFunction (float x)
{
return (x - 1.1f)*(x + 2.2f);
}
//----------------------------------------------------------------------------
|