Welcome to mirror list, hosted at ThFree Co, Russian Federation.

zbufferdatastruct.c « source « intern « render « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f8e683d46bdba810ebd37ecb25055e85a0c98c07 (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
 * zbufferdatastruct.c
 *
 * $Id$
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 *
 * The z buffer consists of an array of lists. Each list holds the objects 
 * behind a pixel. These can be sorted for closest distance. Per object,
 * we store: 
 * - object type
 * - object index
 * - minimum distance
 * - maximum distance
 * - oversample flags
 *
 * The buffer was created to fit the new unified renderpipeline. We might 
 * turn it into an object later on.
 *
 * The z buffer has an unlimited depth. The oversampling code chops at a 
 * certain number of faces. This number is defined in 
 * vanillaRenderPipe_types.h
 *
 * Version 1 of the z buffer inserted objects by means of linear
 * search: we walk along the list until we find the right object or
 * until we have to insert a new one. This is terribly inefficient
 * when we are dealing with large numbers of objects. Can we find a
 * better solution here?
 *
 * Because we treat halos as billboards, we optimize halo
 * insertion. For this purpose the fillFlatObject() functions have
 * been implemented.  */

#include <string.h>

#include "MEM_guardedalloc.h"
#include "zbufferdatastruct.h"
#include "render.h"
#include "render_intern.h"
/* if defined: all jittersamples are stored individually. _very_ serious     */
/* performance hit ! also gives some buffer size problems in big scenes      */
/* #define RE_INDIVIDUAL_SUBPIXELS */

/* ------------------------------------------------------------------------- */

static RE_APixstrExtMain RE_apsemfirst; /* pixstr bookkeeping var            */
static short RE_apsemteller = 0;        /* pixstr bookkeeping var            */
static int RE_zbufferwidth;             /* width of the z-buffer (pixels)    */
RE_APixstrExt *APixbufExt;     /* Zbuffer: linked list of face, halo indices */

/*-RE_APixstrExt------------------------------------------------------------ */ 

void initZbuffer(int width)
{
    APixbufExt         = MEM_callocN(RE_ZBUFLEN * width * sizeof(RE_APixstrExt), 
								 "APixbufExt");
	RE_zbufferwidth    = width;
    RE_apsemteller     = 0;
    RE_apsemfirst.next = NULL;
    RE_apsemfirst.ps   = NULL;
} /* end of RE_APixstrExt *initZbufferDataStruct() */

/* ------------------------------------------------------------------------- */

void freeZbuffer(void)
{
	if (APixbufExt) MEM_freeN(APixbufExt); 
	freepseA(); 
} /* end of void freeZbuffer(void) */

/* ------------------------------------------------------------------------- */

void resetZbuffer(void)
{
	int len;

	freepseA(); 
	len = sizeof(RE_APixstrExt) * RE_zbufferwidth * RE_ZBUFLEN;
	memset(APixbufExt, 0, len);
} /* end of void resetZbuffer(void) */

/* ------------------------------------------------------------------------- */

RE_APixstrExt *addpsemainA()
{
	RE_APixstrExtMain *psm;

	psm= &RE_apsemfirst;

	while(psm->next) {
		psm= psm->next;
	}

	psm->next= MEM_callocN(sizeof(RE_APixstrExtMain), "addpsemainA");

	psm= psm->next;

   /* Initialise the new structure to safe values. Memory that is newly */
   /* allocated must be zero... Not sure if that happens everywhere now.*/
	psm->next=0;
	psm->ps= MEM_callocN(4096*sizeof(RE_APixstrExt),"pixstrext");
	RE_apsemteller= 0;

	return psm->ps;
} /* End of RE_APixstrExt *addpsemainA() */

/* ------------------------------------------------------------------------- */

void freepseA()
{
	RE_APixstrExtMain *psm, *next;

	psm= &RE_apsemfirst;

	while(psm) {
		next= psm->next;
		if(psm->ps) {
			MEM_freeN(psm->ps);
			psm->ps= 0;
		}
		if(psm!= &RE_apsemfirst) MEM_freeN(psm);
		psm= next;
	}

	RE_apsemfirst.next= 0;
	RE_apsemfirst.ps= 0;
	RE_apsemteller= 0;
} /* End of void freepseA() */

/* ------------------------------------------------------------------------- */

RE_APixstrExt *addpseA(void)
{
	static RE_APixstrExt *prev;

	/* eerste PS maken */
	if((RE_apsemteller & 4095)==0) prev= addpsemainA();
	else prev++;
	RE_apsemteller++;
	
	return prev;
} /* End of RE_APixstrExt *addpseA(void) */

/* ------------------------------------------------------------------------- */

void insertObject(int apteller,
/*  				  int opaque, */
				  int obindex,
				  int obtype, 
				  int dist, 
				  int mask)
{
	/* Guard the insertion if needed? */
  	RE_APixstrExt* apn = &APixbufExt[apteller]; 
	
	while(apn) {
		if(apn->t[0] == RE_NONE) {
			apn->p[0] = obindex; apn->t[0] = obtype;
			apn->zmin[0] = dist; apn->zmax[0] = dist;
			apn->mask[0] = mask;   
			break; 
		}
#ifndef RE_INDIVIDUAL_SUBPIXELS
		if((apn->p[0] == obindex) && (apn->t[0] & obtype)) {
			if(dist < apn->zmin[0]) apn->zmin[0] = dist;
			else if(dist > apn->zmax[0]) apn->zmax[0] = dist;
			apn->mask[0]|= mask; 
			break; 
		} 
#endif
		if(apn->t[1] == RE_NONE) {
			apn->p[1] = obindex; apn->t[1] = obtype;
			apn->zmin[1] = dist; apn->zmax[1] = dist;
			apn->mask[1] = mask;   
			break; 
		}
#ifndef RE_INDIVIDUAL_SUBPIXELS
		if((apn->p[1] == obindex) && (apn->t[1] & obtype)) {
			if(dist < apn->zmin[1]) apn->zmin[1] = dist;
			else if(dist > apn->zmax[1]) apn->zmax[1] = dist;
			apn->mask[1]|= mask; 
			break; 
		} 
#endif
		if(apn->t[2] == RE_NONE) {
			apn->p[2] = obindex; apn->t[2] = obtype;
			apn->zmin[2] = dist; apn->zmax[2] = dist;
			apn->mask[2] = mask;   
			break; 
		}
#ifndef RE_INDIVIDUAL_SUBPIXELS
		if((apn->p[2] == obindex) && (apn->t[2] & obtype)) {
			if(dist < apn->zmin[2]) apn->zmin[2] = dist;
			else if(dist > apn->zmax[2]) apn->zmax[2] = dist;
			apn->mask[2]|= mask; 
			break; 
		} 
#endif
		if(apn->t[3] == RE_NONE) {
			apn->p[3] = obindex; apn->t[3] = obtype;
			apn->zmin[3] = dist; apn->zmax[3] = dist;
			apn->mask[3] = mask;   
			break; 
		}
#ifndef RE_INDIVIDUAL_SUBPIXELS
		if((apn->p[3] == obindex) && (apn->t[3] & obtype)) {
			if(dist < apn->zmin[3]) apn->zmin[3] = dist;
			else if(dist > apn->zmax[3]) apn->zmax[3] = dist;
			apn->mask[3]|= mask; 
			break; 
		} 
#endif
		if(apn->next==0) apn->next= addpseA();
		apn= apn->next;
	}				
} /* end of insertObject(RE_APixstrExt*, int, int, int, int) */

/* ------------------------------------------------------------------------- */

void insertFlatObject(RE_APixstrExt *apn, int obindex, int obtype, int dist, int mask)
{
	while(apn) {
		if(apn->t[0] == RE_NONE) {
			apn->p[0] = obindex; apn->zmin[0] = dist; 
			apn->zmax[0] = dist; apn->mask[0] = mask; 
			apn->t[0] = obtype;  
			break; 
		}
#ifndef RE_INDIVIDUAL_SUBPIXELS
		if( (apn->t[0] & obtype) && (apn->p[0] == obindex)) {
			apn->mask[0]|= mask; break; 
		}
#endif
		if(apn->t[1] == RE_NONE) {
			apn->p[1] = obindex; apn->zmin[1] = dist; 
			apn->zmax[1] = dist; apn->mask[1] = mask; 
			apn->t[1] = obtype;  
			break;
		}
#ifndef RE_INDIVIDUAL_SUBPIXELS
		if( (apn->t[1] & obtype) && (apn->p[1] == obindex)) {
			apn->mask[1]|= mask; break; 
		}
#endif
		if(apn->t[2] == RE_NONE) {
			apn->p[2] = obindex; apn->zmin[2] = dist; 
			apn->zmax[2] = dist; apn->mask[2] = mask; 
			apn->t[2] = obtype;  
			break;
		}
#ifndef RE_INDIVIDUAL_SUBPIXELS
		if( (apn->t[2] & obtype) && (apn->p[2] == obindex)) {
			apn->mask[2]|= mask; break; 
		}
#endif
		if(apn->t[3] == RE_NONE) {
			apn->p[3] = obindex; apn->zmin[3] = dist; 
			apn->zmax[3] = dist; apn->mask[3] = mask; 
			apn->t[3] = obtype;  
			break;
		}
#ifndef RE_INDIVIDUAL_SUBPIXELS
		if( (apn->t[3] & obtype) && (apn->p[3] == obindex)) {
			apn->mask[3]|= mask; break; 
		}
#endif
		if(apn->next==0) apn->next= addpseA();
		apn= apn->next;
	};                    
} /* end of void insertFlatObject(RE_APixstrExt, int, int, int, int)*/

/* ------------------------------------------------------------------------- */
/* This function might be helped by an end-of-list marker                    */
void insertFlatObjectNoOsa(RE_APixstrExt *ap, 
						   int obindex, 
						   int obtype, 
						   int dist, 
						   int mask)
{
	while(ap) {
		if(ap->t[0] == RE_NONE) {
			ap->p[0] = obindex; ap->zmin[0] = dist; 
			ap->zmax[0] = dist; ap->mask[0] = mask; 
			ap->t[0] = obtype;  
			break; 
		}

		if(ap->t[1] == RE_NONE) {
			ap->p[1] = obindex; ap->zmin[1] = dist; 
			ap->zmax[1] = dist; ap->mask[1] = mask; 
			ap->t[1] = obtype;  
			break;
		}

		if(ap->t[2] == RE_NONE) {
			ap->p[2] = obindex; ap->zmin[2] = dist; 
			ap->zmax[2] = dist; ap->mask[2] = mask; 
			ap->t[2] = obtype;  
			break;
		}

		if(ap->t[3] == RE_NONE) {
			ap->p[3] = obindex; ap->zmin[3] = dist; 
			ap->zmax[3] = dist; ap->mask[3] = mask; 
			ap->t[3] = obtype;  
			break;
		}

		if(ap->next==0) ap->next= addpseA();
		ap= ap->next;
	};                    
} /* end of void insertFlatObjectNoOsa(RE_APixstrExt, int, int, int, int)*/

/* ------------------------------------------------------------------------- */

/* EOF */