[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [12209] branches/soc-2007-joeedh/source/ blender: Tiling cache system needed threading locks inserted.

Joseph Eagar joeedh at gmail.com
Thu Oct 4 23:41:37 CEST 2007


Revision: 12209
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=12209
Author:   joeedh
Date:     2007-10-04 23:41:36 +0200 (Thu, 04 Oct 2007)

Log Message:
-----------
Tiling cache system needed threading locks inserted.  Also a system to prevent
tiles from being cached was needed; this is used by the shadow lookup code, and
is serves much the same purpose as threading locks; after all, the only time
a dsm tile is modified is when its cached.  Crashes with more then 1 thread
should no longer happen.

Also the error limit for DSM was too large; this may be a case of 
diameter-vs-radius (as pointed out by breakin on IRC) so I simply halved
it.  That seems to have done the trick.

Modified Paths:
--------------
    branches/soc-2007-joeedh/source/blender/blenkernel/BKE_dsm.h
    branches/soc-2007-joeedh/source/blender/blenkernel/BKE_tile.h
    branches/soc-2007-joeedh/source/blender/blenkernel/intern/tcs_cache.c
    branches/soc-2007-joeedh/source/blender/blenkernel/intern/tcs_dsm.c
    branches/soc-2007-joeedh/source/blender/blenlib/BLI_threads.h
    branches/soc-2007-joeedh/source/blender/blenlib/intern/threads.c
    branches/soc-2007-joeedh/source/blender/render/intern/include/zbuf.h
    branches/soc-2007-joeedh/source/blender/render/intern/source/shadbuf.c
    branches/soc-2007-joeedh/source/blender/render/intern/source/zbuf.c

Modified: branches/soc-2007-joeedh/source/blender/blenkernel/BKE_dsm.h
===================================================================
--- branches/soc-2007-joeedh/source/blender/blenkernel/BKE_dsm.h	2007-10-04 11:22:54 UTC (rev 12208)
+++ branches/soc-2007-joeedh/source/blender/blenkernel/BKE_dsm.h	2007-10-04 21:41:36 UTC (rev 12209)
@@ -41,7 +41,6 @@
 /*Each tile has its own memarena to work better with caching.*/
 typedef struct DSMTile {
 	TCS_Tile tile;
-	DSMFunction **layer_rect;
 
 	DSMFunction **r_rect;
 	DSMFunction **g_rect;
@@ -71,23 +70,19 @@
 	int tsizex, tsizey;
 	int max_depth; /*the maximum number of samples in a single pixel*/
 	int max_layers; /*maximum number of extra visibility functions to store for soft sampling*/
+	
+	struct MemArena *bucketarena;
+	ListBase *buckets;
 } DSMBuffer;
 
 #define MAX_SAMPLELAYERS	32
-#define DSM_FINAL_TILESIZE	25
+#define DSM_FINAL_TILESIZE	32
 #define DSM_TILE_MEMARENASIZE	(1<<18)
 
 /*little bit smaller memarena buffer size for the final diced
   25x25 tiles*/
 #define DSM_TILE_FINALMEMARENASIZE (1<<16)
 
-/*private*/
-/*ok, this was meant for using TCS_MakeBuffer, but that's kindof a bad idea really so
-  instead the dsm creation function uses TCS_InitBuffer*/
-typedef struct DSMParams {
-	void *re, *buf;
-} DSMParams;
-
 void _DSM_maketile(TCS_TileBuffer *self, TCS_TilePool *pool, TCS_Tile *vtile);
 void DSM_MakeTilePool(int maxbytes);
 void DSM_FreeTilePool(void);

Modified: branches/soc-2007-joeedh/source/blender/blenkernel/BKE_tile.h
===================================================================
--- branches/soc-2007-joeedh/source/blender/blenkernel/BKE_tile.h	2007-10-04 11:22:54 UTC (rev 12208)
+++ branches/soc-2007-joeedh/source/blender/blenkernel/BKE_tile.h	2007-10-04 21:41:36 UTC (rev 12209)
@@ -25,13 +25,14 @@
  * 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.
+ * The Original Code is Copyright (C) 2007 by the Blender Foundation.
  * All rights reserved.
  *
  * Contributor(s): Joseph Eagar, created 2007
  *
  * ***** END GPL/BL DUAL LICENSE BLOCK *****
  */
+
 #ifndef BKE_TILE_H
 #define BKE_TILE_H
  
@@ -51,6 +52,8 @@
     for convienence.  If the stack grows too big, a necassary number of tiles
     can be removed from the stack, compressed via level-1 or level-2 zlib 
     zip, then written to disk (if necessary).
+
+	NOTE: never insert tiles into any ListBase list yourself!
   
   - Tiles are never written to disk more then once.
   
@@ -78,8 +81,7 @@
 	unsigned long length, cursor, totallocated;
 } TCS_File;
 
-typedef struct TCS_Tile {
-	struct TCS_Tile *next, *prev;
+typedef struct TCS_TileType {
 	void (*loadFromCache)(struct TCS_Tile *self, TCS_File *file);
 	
 	/*should free client data after writing to disk, but not free
@@ -92,31 +94,35 @@
 	
 	/*returns amount size of diskspace the uncompressed tile would take, in bytes*/
 	unsigned int (*getTileLen)(struct TCS_Tile *self);
+ } TCS_TileType;
+ 
+typedef struct TCS_Tile {
+	struct TCS_Tile *next, *prev;
+	TCS_TileType *type;
 
-	/*returns a pointer to pixel data for this tile.  note that this is not
-	  required to be implemented, for example dsm's don't have it.*/
-	void *(*getTilePixel)(struct TCS_Tile *self, int x, int y, int z);
-	
 	/*length of uncompressed tile in memory.
 	  mem_used is length of part of tile that gets saved/compressed,
 	  struct_overhead is length of the part of the tile that isn't
 	  saved/compressed.*/
 	int mem_used, struct_overhead; 
 	
-	/*-privae variables, do not touch!-*/
+	/*-the rest of these members are privae variables, do not touch!-*/
 	int is_compressed, is_written;
 	TCS_File *file, *compressed_memfile;
 	
 	/*filepos is the cached tile position.  this must *never* be messed up!*/
-	int filepos; 
+	int filepos;
 
+	/*prevents caching if nonzero*/
+	int locked; 
+
 	/*len is for caching the tile len, to avoid constantly recalculating the size*/
 	int len;
 
 	struct TCS_TilePool *pool;
 	struct TCS_TileBuffer *buffer; /*parent buffer*/
- } TCS_Tile;
- 
+} TCS_Tile;
+
 /*pools manage tiles, but the the tiles themselves belong to TCS_TileBuffers.
   This is a generic container structure, so it can hold any tile from
   any buffer, of any buffer type.*/
@@ -124,6 +130,8 @@
 	struct TCS_TilePool *next, *prev;
 	ListBase tiles;
 
+	char name[32];
+
 	ListBase open_files; /*open files*/
 	unsigned long long len; /*length of all files, in bytes.*/
 	int totfiles, curid; /*curid is used for naming files.*/
@@ -160,7 +168,8 @@
  } TCS_TileBuffer;
 
 /*TCS_TileBuffer .flags member flags*/
-/*Read only buffer means once a tile is written once, we
+
+ /*Read only buffer means once a tile is written once, we
   never have to write it to disk again.*/
 #define TCS_READONLY	1
 
@@ -178,16 +187,35 @@
 /*remember to use this name!*/
 extern TCS_TileBuffer	TCS_DeepShadowBuffer;
 
-TCS_TilePool *TCS_MakePool(unsigned long max_mem_bytes);
+TCS_TilePool *TCS_MakePool(unsigned long max_mem_bytes, char *name);
 void TCS_FreePool(TCS_TilePool *pool);
 
 TCS_TileBuffer *TCS_MakeBuffer(TCS_TileBuffer *type, int tilesizex, int tilesizey, 
    int sizex, int sizey, void *data);
 void TCS_InitBuffer(TCS_TileBuffer *type, void *buffer);
 void TCS_FreeBuffer(void *buf);
-/*as may dimensions can be used as needed, others can just be passed in as 0.*/
+
+/*get a tile from the buffer.
+  as may dimensions can be used as needed, others can just be passed in as 0.*/
 void *TCS_GetTile(void *buf, int x, int y, int z);
 
+/*get a tile from the buffer and disable caching
+  on it.  remember to call TCS_UnlockTile when
+  finished processing the tile to re-enable
+  caching for that tile!*/
+void *TCS_GetAndLockTile(void *vbuf, int x, int y, int z);
+
+/* disable caching of a tile.  if uncache
+   is nonzero, the function will uncache the
+   tile if its cached.
+   
+   Use TCS_GetAndLockTile instead of this function
+   if you're using threads.*/
+void TCS_LockTile(void *vtile, int uncache);
+
+/*enable caching of a tile.*/
+void TCS_UnlockTile(void *vtile);
+
 /*This function frees generic data associated with a tile, removes it from its pool, etc,
   but it doesn't actually free the client tile data itself.*/
 void TCS_FreeTile(void *vtile);

Modified: branches/soc-2007-joeedh/source/blender/blenkernel/intern/tcs_cache.c
===================================================================
--- branches/soc-2007-joeedh/source/blender/blenkernel/intern/tcs_cache.c	2007-10-04 11:22:54 UTC (rev 12208)
+++ branches/soc-2007-joeedh/source/blender/blenkernel/intern/tcs_cache.c	2007-10-04 21:41:36 UTC (rev 12209)
@@ -35,6 +35,7 @@
 
 #include "BLI_memarena.h"
 #include "BLI_blenlib.h"
+#include "BLI_threads.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -43,11 +44,17 @@
 #include <zlib.h>
 
 //static ListBase global_tile_pools = {NULL, NULL};
+static void _TCS_RunCacher(TCS_TilePool *pool, int canlock);
 
-TCS_TilePool *TCS_MakePool(unsigned long max_mem_bytes)
+TCS_TilePool *TCS_MakePool(unsigned long max_mem_bytes, char *name)
 {
 	TCS_TilePool *pool = MEM_callocN(sizeof(TCS_TilePool), "tilepool");
 	
+	strncpy(pool->name, name, 32);
+
+	/*sadly, strncpy does *not* do this. . .*/
+	pool->name[31] = 0;
+
 	pool->max_file_len = 512*1024*1024;
 	pool->maxmem = max_mem_bytes;
 	
@@ -120,7 +127,7 @@
 	MEM_freeN(buf);
 }
 
-
+/*
 void *TCS_GetPixel(TCS_TileBuffer *buf, int x, int y)
 {
 	TCS_Tile *tile;
@@ -129,8 +136,9 @@
 	BASSERT(tile);
 	
 	if (!tile) return NULL;
-	return tile->getTilePixel(tile, x%buf->getTileSizeX(buf), y%buf->getTileSizeY(buf), 0);
+	return tile->type->getTilePixel(tile, x%buf->getTileSizeX(buf), y%buf->getTileSizeY(buf), 0);
 }
+*/
 
 //static int TCS_deflate(TCS_File *source, TCS_File *dest, int level);
 //static int TCS_inflate(TCS_File *source, TCS_File *dest, int len);
@@ -139,7 +147,6 @@
 
 static void TCS_CompressMemFile(TCS_File *file)
 {
-#ifdef USECOMPRESSION
 	TCS_File *memfile2 = TCS_openmemfile((float)file->length*1.1+12);
 	int r;
 
@@ -156,12 +163,10 @@
 	file->cursor = memfile2->cursor;
 	
 	MEM_freeN(memfile2);
-#endif
 }
 
 void TCS_DecompressMemFile(TCS_File *file, int tilelen)
 {
-#ifdef USECOMPRESSION
 	TCS_File *memfile;
 	int r;
 
@@ -180,27 +185,26 @@
 	file->totallocated = memfile->totallocated;
 	
 	MEM_freeN(memfile);
-#endif
 }
 
 /*as may dimensions can be used as needed, others can just be passed in as 0.*/
-void *TCS_GetTile(void *vbuf, int x, int y, int z)
+static void *_TCS_UncacheTile(void *vbuf, TCS_Tile *tile)
 {
 	TCS_TileBuffer *buf = vbuf;
-	TCS_Tile *tile = NULL;
 	TCS_TilePool *pool = NULL;
 	
-	BASSERT(vbuf);
-	tile = buf->getTile(buf, x, y, z);
-	//BASSERT(tile);
-	
+	BASSERT(tile);
 	if (!tile) return NULL;
 	
 	pool = tile->pool;
 	BASSERT(pool);
 	
-	if (!pool) return NULL;
-	if (pool->memused > pool->maxmem) TCS_RunCacher(pool);
+	/*if the tile doesn't belong to a pool,
+	  then it was never cached in the first place*/
+	if (!pool) return tile;
+
+	/*run memory cacher on the pool if necassary*/
+	if (pool->memused > pool->maxmem) _TCS_RunCacher(pool, 0);
 	
 	if (tile->is_compressed != 0) {
 		//printf("comp: %p, file: %p, tile->compressed_memfile, tile->file);
@@ -210,26 +214,9 @@
 			unsigned long len;
 			void *mem;
 
-			/*
-			printf("loading a disk tile\n");
-
-			{
-				char s[5];
-				
-				s[4] = 0;
-				TCS_fseek(tile->file, tile->filepos+1, SEEK_SET);
-				TCS_fread(s, 4, 1, tile->file);
-
-				printf("id: %s \n", s);
-			}
-			*/
-
 			TCS_fseek(tile->file, tile->filepos, SEEK_SET);
 			TCS_fread(&len, sizeof(unsigned long), 1, tile->file);
 			
-			#define BLEH
-			#if defined(USE_COMPRESSION) || defined(BLEH)
-			
 			#ifdef TCSDEBUG
 				printf("gettile len: %lud\n", len);
 			#endif
@@ -243,26 +230,19 @@
 			#endif
 				G.afbreek = 1;
 				if (mem) MEM_freeN(mem);
+
+				BLI_unlock_thread(LOCK_CACHER);
 				return NULL;
 			}
 
-			//TCS_fwrite(mem, len, 1, memfile);
 			memfile = TCS_openmemfile(0);
 			memfile->ptr = mem; memfile->length = len; memfile->cursor = 0; memfile->totallocated = len;
 			
 			TCS_DecompressMemFile(memfile, tile->len);
-			tile->loadFromCache(tile, memfile);
+			tile->type->loadFromCache(tile, memfile);
 			TCS_fclose(memfile);			
 
-			#else
-			mem = NULL;
-			memfile = NULL;
-
-			tile->loadFromCache(tile, tile->file);
-			#endif
-			
 			pool->memused += tile->mem_used;
-			//tile->loadFromCache(tile, file);
 			BASSERT(tile->compressed_memfile == NULL);
 		} else {

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list