1 /*******************************************************************************
2 * threaddata.cpp
3 *
4 * ---------------------------------------------------------------------------
5 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
6 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
7 *
8 * POV-Ray is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * POV-Ray is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * ---------------------------------------------------------------------------
21 * POV-Ray is based on the popular DKB raytracer version 2.12.
22 * DKBTrace was originally written by David K. Buck.
23 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
24 * ---------------------------------------------------------------------------
25 * $File: //depot/public/povray/3.x/source/backend/scene/threaddata.cpp $
26 * $Revision: #1 $
27 * $Change: 6069 $
28 * $DateTime: 2013/11/06 11:59:40 $
29 * $Author: chrisc $
30 *******************************************************************************/
31
32 // frame.h must always be the first POV file included (pulls in platform config)
33 #include "backend/frame.h"
34 #include "backend/scene/threaddata.h"
35 #include "backend/scene/view.h"
36 #include "backend/scene/scene.h"
37 #include "backend/scene/objects.h"
38 #include "backend/shape/fractal.h"
39 #include "backend/shape/blob.h"
40 #include "backend/shape/isosurf.h"
41 #include "backend/texture/texture.h"
42 #include "backend/vm/fnpovfpu.h"
43 #include "backend/bounding/bcyl.h"
44 #include "backend/support/statistics.h"
45
46 // this must be the last file included
47 #include "base/povdebug.h"
48
49 namespace pov
50 {
51
SceneThreadData(shared_ptr<SceneData> sd)52 SceneThreadData::SceneThreadData(shared_ptr<SceneData> sd): sceneData(sd)
53 {
54 for(int i = 0 ; i < 4 ; i++)
55 Fractal_IStack[i] = NULL;
56 Fractal::Allocate_Iteration_Stack(Fractal_IStack, sceneData->Fractal_Iteration_Stack_Length);
57 Max_Blob_Queue_Size = 1;
58 Blob_Coefficient_Count = sceneData->Max_Blob_Components * 5;
59 Blob_Interval_Count = sceneData->Max_Blob_Components * 2;
60 Blob_Queue = reinterpret_cast<void **>(POV_MALLOC(sizeof(void **), "Blob Queue"));
61 Blob_Coefficients = reinterpret_cast<DBL *>(POV_MALLOC(sizeof(DBL) * Blob_Coefficient_Count, "Blob Coefficients"));
62 Blob_Intervals = new Blob_Interval_Struct [Blob_Interval_Count];
63 isosurfaceData = reinterpret_cast<ISO_ThreadData *>(POV_MALLOC(sizeof(ISO_ThreadData), "Isosurface Data"));
64 isosurfaceData->ctx = NULL;
65 isosurfaceData->current = NULL;
66 isosurfaceData->cache = false;
67 isosurfaceData->Inv3 = 1;
68 isosurfaceData->fmax = 0.0;
69 isosurfaceData->tl = 0.0;
70 isosurfaceData->Vlength = 0.0;
71
72 functionContext = sceneData->functionVM->NewContext(this);
73 functionPatternContext.resize(sceneData->functionPatternCount);
74
75 BCyl_Intervals = POV_MALLOC (4*sceneData->Max_Bounding_Cylinders*sizeof(BCYL_INT), "lathe intersection list");
76 BCyl_RInt = POV_MALLOC (2*sceneData->Max_Bounding_Cylinders*sizeof(BCYL_INT), "lathe intersection list");
77 BCyl_HInt = POV_MALLOC (2*sceneData->Max_Bounding_Cylinders*sizeof(BCYL_INT), "lathe intersection list");
78
79 Facets_Last_Seed = 0x80000000;
80
81 timeType = kUnknownTime;
82 cpuTime = 0;
83 realTime = 0;
84
85 qualityFlags = QUALITY_9;
86
87 for(vector<LightSource *>::iterator it = sceneData->lightSources.begin(); it != sceneData->lightSources.end(); it++)
88 lightSources.push_back(static_cast<LightSource *> (Copy_Object(*it))) ;
89
90 // all of these are for photons
91 LightSource *photonLight = NULL;
92 ObjectPtr photonObject = NULL;
93 litObjectIgnoresPhotons = false;
94 hitObject = false; // did we hit the target object? (for autostop)
95 photonSpread = 0.0; // photon spread (in radians)
96 photonDepth = 0.0; // total distance from light to intersection
97 passThruThis = false; // is this a pass-through object?
98 passThruPrev = false; // was the previous object pass-through?
99 Light_Is_Global = false; // is the current light global? (not part of a light_group?)
100
101 CrCache_MaxAge = 1;
102 progress_index = 0;
103
104 surfacePhotonMap = new PhotonMap();
105 mediaPhotonMap = new PhotonMap();
106
107 // advise the crackle cache's unordered_map that we don't mind hash collisions
108 // while this is a very high load factor, the simple fact is that the cost of
109 // allocating memory at render time (each insert into the table requires an alloc
110 // as the container doesn't pre-emptively allocate, unlike e.g. std::vector) is
111 // quite high, particularly when we have multiple threads contending for the heap
112 // lock.
113 Crackle_Cache.max_load_factor(50.0);
114
115 numberOfWaves = sd->numberOfWaves;
116 Initialize_Waves(waveFrequencies, waveSources, numberOfWaves);
117 }
118
~SceneThreadData()119 SceneThreadData::~SceneThreadData()
120 {
121 sceneData->functionVM->DeleteContext(functionContext);
122
123 POV_FREE(BCyl_HInt);
124 POV_FREE(BCyl_RInt);
125 POV_FREE(BCyl_Intervals);
126 POV_FREE(Blob_Coefficients);
127 POV_FREE(Blob_Queue);
128 POV_FREE(isosurfaceData);
129 Fractal::Free_Iteration_Stack(Fractal_IStack);
130 delete surfacePhotonMap;
131 delete mediaPhotonMap;
132 delete[] Blob_Intervals;
133 for(vector<LightSource *>::iterator it = lightSources.begin(); it != lightSources.end(); it++)
134 Destroy_Object(*it);
135 }
136
AfterTile()137 void SceneThreadData::AfterTile()
138 {
139 Crackle_Cache_Type::iterator it;
140
141 // this serves as a render block index
142 progress_index++;
143
144 // probably we ought to have a means for the end-user to choose the preferred maximum bytes reserved for the cache
145 // for now, we have hard-coded values. we also do not discard any entries that are from the current block, even if
146 // the cache size is exceeded. also, note that the cache size is per-thread. finally, don't forget that erasing
147 // elements doesn't in and of itself return the freed memory to the heap.
148 if (Crackle_Cache.size() * sizeof(Crackle_Cache_Type::value_type) < 15 * 1024 * 1024)
149 return;
150 while (Crackle_Cache.size() * sizeof(Crackle_Cache_Type::value_type) > 10 * 1024 * 1024)
151 {
152 // search the cache for the oldest entries
153 int oldest = std::numeric_limits<int>::max();
154 for (it = Crackle_Cache.begin(); it != Crackle_Cache.end(); it++)
155 if (it->second.last_used < oldest)
156 oldest = (int) it->second.last_used;
157
158 // don't remove any entries from the most recent block
159 if (oldest == progress_index - 1)
160 break;
161
162 for (it = Crackle_Cache.begin(); it != Crackle_Cache.end(); )
163 {
164 if (it->second.last_used == oldest)
165 {
166 it = Crackle_Cache.erase(it);
167 continue;
168 }
169 it++;
170 }
171 }
172 }
173
ViewThreadData(ViewData * vd)174 ViewThreadData::ViewThreadData(ViewData *vd) :
175 SceneThreadData(vd->GetSceneData()),
176 viewData(vd)
177 {
178 }
179
~ViewThreadData()180 ViewThreadData::~ViewThreadData()
181 {
182 }
183
GetWidth() const184 unsigned int ViewThreadData::GetWidth() const
185 {
186 return viewData->GetWidth();
187 }
188
GetHeight() const189 unsigned int ViewThreadData::GetHeight() const
190 {
191 return viewData->GetHeight();
192 }
193
GetRenderArea()194 const POVRect& ViewThreadData::GetRenderArea()
195 {
196 return viewData->GetRenderArea();
197 }
198
199 }
200