File: radiositytask.cpp

package info (click to toggle)
povray 1%3A3.7.0.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,388; cs: 879; awk: 590; perl: 245; xml: 95
file content (258 lines) | stat: -rw-r--r-- 9,095 bytes parent folder | download | duplicates (6)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*******************************************************************************
 * radiositytask.cpp
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 * ---------------------------------------------------------------------------
 * $File: //depot/public/povray/3.x/source/backend/render/radiositytask.cpp $
 * $Revision: #1 $
 * $Change: 6069 $
 * $DateTime: 2013/11/06 11:59:40 $
 * $Author: chrisc $
 *******************************************************************************/

#include <boost/thread.hpp>
#include <boost/bind.hpp>

// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"

#include "base/types.h"
#include "base/timer.h"

#include "backend/render/radiositytask.h"
#include "backend/scene/threaddata.h"
#include "backend/scene/scene.h"
#include "backend/scene/view.h"

// this must be the last file included
#include "base/povdebug.h"

namespace pov
{

using namespace pov_base;

RadiosityTask::RadiosityTask(ViewData *vd, DBL ptsz, DBL ptesz, unsigned int pts, unsigned int ptsc, unsigned int nt) :
	RenderTask(vd),
	trace(vd, GetViewDataPtr(), vd->GetSceneData()->parsedMaxTraceLevel, vd->GetSceneData()->parsedAdcBailout,
	      vd->GetQualityFeatureFlags(), cooperate, media, radiosity, !vd->GetSceneData()->radiositySettings.vainPretrace),
	cooperate(*this),
	media(GetViewDataPtr(), &trace, &photonGatherer),
	radiosity(vd->GetSceneData(), GetViewDataPtr(),
	          vd->GetSceneData()->radiositySettings, vd->GetRadiosityCache(), cooperate, false, Vector3d(vd->GetCamera().Location)),
	photonGatherer(&vd->GetSceneData()->surfacePhotonMap, vd->GetSceneData()->photonSettings),
	pretraceStep(pts),
	pretraceStepCount(ptsc),
	pretraceStartSize(ptsz),
	pretraceEndSize(ptesz),
	pretraceCoverage(vd->GetSceneData()->radiositySettings.nearestCountAPT),
	nominalThreads(nt)
{
}

RadiosityTask::~RadiosityTask()
{
}

void RadiosityTask::Run()
{
	//RandomIntSequence rands(0, 2, 2047);
	//RandomIntSequence::Generator randgen(&rands);
	RandomDoubleSequence rands(-1.0, 1.0, 2047);
	RandomDoubleSequence::Generator randgen(&rands);

	DBL width = GetViewData()->GetWidth();
	DBL height = GetViewData()->GetHeight();

	POVRect rect;
	vector<Vector2d> pixelpositions;
	vector<Colour> pixelcolors;
	unsigned int serial;

	float progressWeightTotal = (pow(4.0f, (float)pretraceStepCount) - 1.0) / 3.0; // equal to SUM[i=0,N-1](pow(4.0, i))

	ViewData::BlockInfo* pInfo;

	while(GetViewData()->GetNextRectangle(rect, serial, pInfo, nominalThreads) == true)
	{
		RadiosityBlockInfo* pBlockInfo = dynamic_cast<RadiosityBlockInfo*>(pInfo);
		if (!pBlockInfo)
		{
			if (pInfo)
			{
				delete pInfo;
				pInfo = NULL;
			}
			pBlockInfo = new RadiosityBlockInfo();
		}

		unsigned int currentStep = pretraceStep + pBlockInfo->pass;
		double pretraceSize     = max(pretraceStartSize * pow(0.5f, (float)pBlockInfo->pass),     pretraceEndSize);
		double nextPretraceSize = max(pretraceStartSize * pow(0.5f, (float)pBlockInfo->pass + 1), pretraceEndSize);

		radiosity.BeforeTile((nominalThreads? serial % nominalThreads : 0), pretraceStep + pBlockInfo->pass);
		randgen.SetSeed((pretraceStep + pBlockInfo->pass) * 17 + serial * 13); // make sure our jitter is different (but reproducible) for each pass and tile

		unsigned int px = (rect.GetWidth()  + pretraceSize - 1) / pretraceSize;
		unsigned int py = (rect.GetHeight() + pretraceSize - 1) / pretraceSize;

		unsigned int nextPx = (rect.GetWidth()  + nextPretraceSize - 1) / nextPretraceSize;
		unsigned int nextPy = (rect.GetHeight() + nextPretraceSize - 1) / nextPretraceSize;

		double startX   = ceil((DBL(rect.left)  - 0.5) / pretraceSize) * pretraceSize;
		double startY   = ceil((DBL(rect.top)   - 0.5) / pretraceSize) * pretraceSize;
		double endX     = DBL(rect.right)       + 0.5;
		double endY     = DBL(rect.bottom)      + 0.5;

		// make sure we start with the pixel buffer cleared
		pixelpositions.clear();
		pixelpositions.reserve(px * py);
		pixelcolors.clear();
		pixelcolors.reserve(px * py);

		double jitter = min(1.0, pretraceSize / 2.0);
		double offset = (pretraceSize - 1.0) / 2.0;
		unsigned int subBlockCount = pBlockInfo->incompleteSubBlocks.size();

		int subBlockDivideX;
		int subBlockDivideY;

		if (pretraceCoverage != 0)
		{
			// for the next pass, subdivide further as long as that this leaves us with at least 4x4 pixels
			subBlockDivideX = max( 1, (int)floor((DBL)nextPx / (DBL)(pBlockInfo->subBlockCountX * 4)) );
			subBlockDivideY = max( 1, (int)floor((DBL)nextPy / (DBL)(pBlockInfo->subBlockCountY * 4)) );
		}
		else
		{
			// don't subdivide if we're not using adaptive pretrace
			subBlockDivideX = 1;
			subBlockDivideY = 1;
		}

		for (int sub = 0; sub < subBlockCount; sub ++)
		{

			radiosity.ResetTopLevelStats();
			int pixelCount = 0;

			int subX = pBlockInfo->incompleteSubBlocks.front().subBlockPosX;
			int subY = pBlockInfo->incompleteSubBlocks.front().subBlockPosY;

			DBL subStartX = rect.left + (DBL)rect.GetWidth()  * (DBL) subX / (DBL)(pBlockInfo->subBlockCountX);
			DBL subEndX   = subStartX + (DBL)rect.GetWidth()               / (DBL)(pBlockInfo->subBlockCountX);
			DBL subStartY = rect.top  + (DBL)rect.GetHeight() * (DBL) subY / (DBL)(pBlockInfo->subBlockCountY);
			DBL subEndY   = subStartY + (DBL)rect.GetHeight()              / (DBL)(pBlockInfo->subBlockCountY);

			for(DBL y = startY; y < subEndY; y += pretraceSize)
			{
				if (y < subStartY)
					continue;

				for(DBL x = startX; x < subEndX; x += pretraceSize)
				{
					if (x < subStartX)
						continue;

					Colour col;

					trace(x + offset + jitter * randgen(), y + offset + jitter * randgen(), width, height, col);

					pixelpositions.push_back(Vector2d(x, y));
					pixelcolors.push_back(col);
					pixelCount ++;

					Cooperate();
				}
			}

			long  queryCount;
			float reuse;
			radiosity.GetTopLevelStats(queryCount, reuse);

			bool again;
			if (pixelCount < 9)
				// shoot at least a certain number of rays
				// (NB: Subdivision strategy tries to ensure we get at least 4x4 pixels)
				again = true;
			else if (queryCount == 0)
				// stop if we don't seem to need any samples at all in this square (e.g. because it shows only background)
				again = false;
			else if ((pretraceCoverage != 0) && (reuse / (float)queryCount >= pretraceCoverage))
				// stop if the average number of re-usable samples reaches a certain threshold
				again = false;
			else
				// otherwise do another pass
				again = true;

			pBlockInfo->incompleteSubBlocks.pop_front();
			if (again)
			{
				for (int subDivY = 0; subDivY < subBlockDivideY; subDivY ++)
					for (int subDivX = 0; subDivX < subBlockDivideX; subDivX ++)
						pBlockInfo->incompleteSubBlocks.push_back(RadiositySubBlockInfo(subX * subBlockDivideX + subDivX, subY * subBlockDivideY + subDivY));
			}
		}
		GetViewDataPtr()->Stats()[Number_Of_Pixels] += pixelpositions.size();

		radiosity.AfterTile();

		float progressWeight = pow(4.0f, (float)pBlockInfo->pass) / progressWeightTotal;

		pBlockInfo->pass ++;

		if (pBlockInfo->pass < pretraceStepCount && pBlockInfo->incompleteSubBlocks.size() > 0)
		{
			// run another pass
			pBlockInfo->subBlockCountX *= subBlockDivideX;
			pBlockInfo->subBlockCountY *= subBlockDivideY;
			pBlockInfo->completion += progressWeight;
		}
		else
		{
			// no more passes please
			progressWeight = 1.0 - pBlockInfo->completion;
			delete pBlockInfo;
			pBlockInfo = NULL;
		}

		GetViewDataPtr()->AfterTile();
		if(pixelpositions.size() > 0)
			GetViewData()->CompletedRectangle(rect, serial, pixelpositions, pixelcolors, int(ceil(pretraceSize)), false, progressWeight, pBlockInfo);
		else
			GetViewData()->CompletedRectangle(rect, serial, progressWeight, pBlockInfo);
	}
}

void RadiosityTask::Stopped()
{
	// nothing to do for now [trf]
}

void RadiosityTask::Finish()
{
	GetViewDataPtr()->timeType = SceneThreadData::kRadiosityTime;
	GetViewDataPtr()->realTime = ConsumedRealTime();
	GetViewDataPtr()->cpuTime = ConsumedCPUTime();
}

}