Skip to content

Commit

Permalink
Squeak3D/B3DEnginePlugin as per Balloon3D-Plugins-eem.15
Browse files Browse the repository at this point in the history
Simplify b3dInitializeRasterizerState, reducing the number of primitiveFailed
calls and eliminating cCode:'s.

Refactor the support code to make clear that allocations only happen in
b3dMain.c.  Fix an uncheck allocation bug that crashes the plugin with very
complex scenes.
  • Loading branch information
eliotmiranda committed Nov 12, 2018
1 parent 897ef17 commit 745fbbf
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 105 deletions.
37 changes: 36 additions & 1 deletion platforms/Cross/plugins/Squeak3D/b3d.h
Expand Up @@ -25,8 +25,43 @@

/* primary include file */

#include "sqAssert.h"
#include "b3dTypes.h"
#include "b3dAlloc.h"

/************************ Allocator definitions ************************/
#define B3D_EDGE_ALLOC_MAGIC 0x45443341
typedef struct B3DEdgeAllocList {
int magic;
void *This;
int max; /* Note: size is ALWAYS less than max */
int size;
int nFree;
B3DPrimitiveEdge *firstFree; /* pointer to the first free edge (< max) */
B3DPrimitiveEdge data[1];
} B3DEdgeAllocList;

#define B3D_FACE_ALLOC_MAGIC 0x46443341
typedef struct B3DFaceAllocList {
int magic;
void *This;
int max; /* Note: size is ALWAYS less than max */
int size;
int nFree;
B3DPrimitiveFace *firstFree; /* pointer to the first free face (< max) */
B3DPrimitiveFace data[1];
} B3DFaceAllocList;

#define B3D_ATTR_ALLOC_MAGIC 0x41443341
typedef struct B3DAttrAllocList {
int magic;
void *This;
int max; /* Note: size is ALWAYS less than max */
int size;
int nFree;
B3DPrimitiveAttribute *firstFree; /* pointer to the first free attribute (< max) */
B3DPrimitiveAttribute data[1];
} B3DAttrAllocList;
/************************ End Allocator definitions ************************/

typedef int (*b3dDrawBufferFunction) (int leftX, int rightX, int yValue);

Expand Down
36 changes: 2 additions & 34 deletions platforms/Cross/plugins/Squeak3D/b3dAlloc.h
Expand Up @@ -17,40 +17,6 @@

#include "b3dTypes.h"

/************************ Allocator definitions ************************/
#define B3D_EDGE_ALLOC_MAGIC 0x45443341
typedef struct B3DEdgeAllocList {
int magic;
void *This;
int max; /* Note: size is ALWAYS less than max */
int size;
int nFree;
B3DPrimitiveEdge *firstFree; /* pointer to the first free edge (< max) */
B3DPrimitiveEdge data[1];
} B3DEdgeAllocList;

#define B3D_FACE_ALLOC_MAGIC 0x46443341
typedef struct B3DFaceAllocList {
int magic;
void *This;
int max; /* Note: size is ALWAYS less than max */
int size;
int nFree;
B3DPrimitiveFace *firstFree; /* pointer to the first free face (< max) */
B3DPrimitiveFace data[1];
} B3DFaceAllocList;

#define B3D_ATTR_ALLOC_MAGIC 0x41443341
typedef struct B3DAttrAllocList {
int magic;
void *This;
int max; /* Note: size is ALWAYS less than max */
int size;
int nFree;
B3DPrimitiveAttribute *firstFree; /* pointer to the first free attribute (< max) */
B3DPrimitiveAttribute data[1];
} B3DAttrAllocList;

/* The mapping from face flags to the number of attributes needed */
extern int B3D_ATTRIBUTE_SIZES[B3D_MAX_ATTRIBUTES];
#define B3D_FACE_ATTRIB_SIZE(face) (B3D_ATTRIBUTE_SIZES[(face->flags >> B3D_ATTR_SHIFT) & B3D_ATTR_MASK])
Expand All @@ -75,6 +41,7 @@ void dbg_b3dFreeAttrib(B3DAttrAllocList *list, B3DPrimitiveFace *face);

#define b3dAlloc(list,object) \
{\
assert(list->size <= list->max); \
if(list->firstFree) { \
object = list->firstFree; \
list->firstFree = object->nextFree; \
Expand Down Expand Up @@ -105,6 +72,7 @@ void dbg_b3dFreeAttrib(B3DAttrAllocList *list, B3DPrimitiveFace *face);

#define b3dAllocSingleAttr(list,object) \
{\
assert(list->size <= list->max); \
if(list->firstFree) { \
object = list->firstFree; \
list->firstFree = object->next; \
Expand Down
33 changes: 26 additions & 7 deletions platforms/Cross/plugins/Squeak3D/b3dMain.c
Expand Up @@ -14,11 +14,31 @@
*****************************************************************************/
#include <stdio.h> /* printf() */
#include <stdlib.h> /* exit() */
#include <assert.h> /* assert() */
#if 0
# include <assert.h> /* assert() */
#endif
#include "b3d.h"
#include "b3dAlloc.h"

#ifndef NULL
#define NULL ((void*)0)
# define NULL ((void*)0)
#endif

#if !defined(SQUEAK_BUILTIN_PLUGIN) && !defined(NDEBUG)
/* A warning for sqAssert.h. Ideally we would use the one in the interpreter.
* This is written crappily because we don't yet have warning exported properly
* to dlls on Windows. Do we export warning via __declspec or not?
*/
void
warning(char *s) { /* Print an error message but don't necessarily exit. */
# if 0
if (erroronwarn) error(s);
if (warnpid)
printf("\n%s pid %ld\n", s, (long)warnpid);
else
# endif
printf("\n%s\n", s);
}
#endif

#ifdef B3D_PROFILE
Expand Down Expand Up @@ -132,8 +152,8 @@ B3DPrimitiveFace *b3dInitializeFace(B3DPrimitiveVertex *v0,
/* Now that we know the face is valid, do the actual allocation */
b3dAllocFace(faceAlloc, face);

if(b3dDebug)
if(!face) b3dAbort("Face allocation failed");
if(!face)
return NULL;

face->v0 = v0;
face->v1 = v1;
Expand Down Expand Up @@ -455,9 +475,8 @@ B3DPrimitiveEdge *b3dAddLowerEdgeFromFace(B3DPrimitiveFace *face, B3DPrimitiveEd
if(!nLines) return NULL; /* Edge is horizontal */
b3dAllocEdge(edgeAlloc, minorEdge);

if(b3dDebug)
if(!minorEdge)
b3dAbort("Edge allocation failed");
if(!minorEdge)
return NULL;

minorEdge->v0 = v1;
minorEdge->v1 = v2;
Expand Down
1 change: 0 additions & 1 deletion platforms/Cross/plugins/Squeak3D/b3dTypes.h
Expand Up @@ -318,7 +318,6 @@ typedef struct B3DPrimitiveObject {

int nSortedFaces;
int nInvalidFaces;


int start;
int nFaces;
Expand Down
83 changes: 21 additions & 62 deletions src/plugins/Squeak3D/Squeak3D.c
@@ -1,9 +1,9 @@
/* Automatically generated by
VMPluginCodeGenerator VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
VMPluginCodeGenerator VMMaker.oscog-eem.2484 uuid: 63db7375-cf0b-4a19-86d9-9e4a4da68117
from
B3DEnginePlugin Balloon3D-Plugins-eem.14 uuid: ab42f50d-ede8-49e4-9b08-b92eb60c776b
B3DEnginePlugin Balloon3D-Plugins-eem.15 uuid: d694149b-363b-4754-aa4a-7ef0cb2c5eab
*/
static char __buildInfo[] = "B3DEnginePlugin Balloon3D-Plugins-eem.14 uuid: ab42f50d-ede8-49e4-9b08-b92eb60c776b " __DATE__ ;
static char __buildInfo[] = "B3DEnginePlugin Balloon3D-Plugins-eem.15 uuid: d694149b-363b-4754-aa4a-7ef0cb2c5eab " __DATE__ ;



Expand Down Expand Up @@ -220,7 +220,6 @@ static void * (*firstIndexableField)(sqInt oop);
static sqInt (*floatObjectOf)(double aFloat);
static void * (*ioLoadFunctionFrom)(char *functionName, char *moduleName);
static sqInt (*isBytes)(sqInt oop);
static sqInt (*isIntegerObject)(sqInt objectPointer);
static sqInt (*isPointers)(sqInt oop);
static sqInt (*isWords)(sqInt oop);
static sqInt (*methodArgumentCount)(void);
Expand Down Expand Up @@ -253,9 +252,6 @@ extern void * firstIndexableField(sqInt oop);
extern sqInt floatObjectOf(double aFloat);
extern void * ioLoadFunctionFrom(char *functionName, char *moduleName);
extern sqInt isBytes(sqInt oop);
#if !defined(isIntegerObject)
extern sqInt isIntegerObject(sqInt objectPointer);
#endif
extern sqInt isPointers(sqInt oop);
extern sqInt isWords(sqInt oop);
extern sqInt methodArgumentCount(void);
Expand Down Expand Up @@ -285,9 +281,9 @@ static float* litVertex;
static sqInt loadBBFn;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
"Squeak3D Balloon3D-Plugins-eem.14 (i)"
"Squeak3D Balloon3D-Plugins-eem.15 (i)"
#else
"Squeak3D Balloon3D-Plugins-eem.14 (e)"
"Squeak3D Balloon3D-Plugins-eem.15 (e)"
#endif
;
static float* primLight;
Expand Down Expand Up @@ -757,87 +753,51 @@ b3dDetermineClipFlags(void)


/* Primitive. Initialize the primitive level objects of the given rasterizer. */
/* Check argument count */

/* B3DRasterizerPlugin>>#b3dInitializeRasterizerState */
EXPORT(sqInt)
b3dInitializeRasterizerState(void)
{
void *obj;
sqInt objLen;
sqInt objOop;
sqInt stateOop;


/* Check argument count */
if (!((methodArgumentCount()) == 0)) {
return primitiveFail();
}
stateOop = stackObjectValue(0);
if (failed()) {
return null;
}
stateOop = stackValue(0);
if (!((isPointers(stateOop))
&& ((slotSizeOf(stateOop)) >= 7))) {
return primitiveFail();
}
objOop = fetchPointerofObject(0, stateOop);
if ((isIntegerObject(objOop))
|| (!(isWords(objOop)))) {
return primitiveFail();
}
objLen = byteSizeOf(objOop);
obj = firstIndexableField(objOop);
if (b3dInitializeFaceAllocator(obj, objLen) != B3D_NO_ERROR) {
if (!((isWords(objOop))
&& ((b3dInitializeFaceAllocator(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(1, stateOop);
if ((isIntegerObject(objOop))
|| (!(isWords(objOop)))) {
return primitiveFail();
}
objLen = byteSizeOf(objOop);
obj = firstIndexableField(objOop);
if (b3dInitializeEdgeAllocator(obj, objLen) != B3D_NO_ERROR) {
if (!((isWords(objOop))
&& ((b3dInitializeEdgeAllocator(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(2, stateOop);
if ((isIntegerObject(objOop))
|| (!(isWords(objOop)))) {
return primitiveFail();
}
objLen = byteSizeOf(objOop);
obj = firstIndexableField(objOop);
if (b3dInitializeAttrAllocator(obj, objLen) != B3D_NO_ERROR) {
if (!((isWords(objOop))
&& ((b3dInitializeAttrAllocator(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(3, stateOop);
if ((isIntegerObject(objOop))
|| (!(isWords(objOop)))) {
return primitiveFail();
}
objLen = byteSizeOf(objOop);
obj = firstIndexableField(objOop);
if (b3dInitializeAET(obj, objLen) != B3D_NO_ERROR) {
if (!((isWords(objOop))
&& ((b3dInitializeAET(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(4, stateOop);
if ((isIntegerObject(objOop))
|| (!(isWords(objOop)))) {
return primitiveFail();
}
objLen = byteSizeOf(objOop);
obj = firstIndexableField(objOop);
if (b3dInitializeEdgeList(obj, objLen) != B3D_NO_ERROR) {
if (!((isWords(objOop))
&& ((b3dInitializeEdgeList(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(5, stateOop);
if ((isIntegerObject(objOop))
|| (!(isWords(objOop)))) {
return primitiveFail();
}
objLen = byteSizeOf(objOop);
obj = firstIndexableField(objOop);
if (b3dInitializeFillList(obj, objLen) != B3D_NO_ERROR) {
if (!((isWords(objOop))
&& ((b3dInitializeFillList(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
return 0;
Expand Down Expand Up @@ -3460,7 +3420,6 @@ setInterpreter(struct VirtualMachine *anInterpreter)
floatObjectOf = interpreterProxy->floatObjectOf;
ioLoadFunctionFrom = interpreterProxy->ioLoadFunctionFrom;
isBytes = interpreterProxy->isBytes;
isIntegerObject = interpreterProxy->isIntegerObject;
isPointers = interpreterProxy->isPointers;
isWords = interpreterProxy->isWords;
methodArgumentCount = interpreterProxy->methodArgumentCount;
Expand Down Expand Up @@ -4235,7 +4194,7 @@ void* Squeak3D_exports[][3] = {
{(void*)_m, "b3dComputeMinIndexZ\000\001", (void*)b3dComputeMinIndexZ},
{(void*)_m, "b3dComputeMinZ\000\001", (void*)b3dComputeMinZ},
{(void*)_m, "b3dDetermineClipFlags\000\001", (void*)b3dDetermineClipFlags},
{(void*)_m, "b3dInitializeRasterizerState\000\002", (void*)b3dInitializeRasterizerState},
{(void*)_m, "b3dInitializeRasterizerState\000\001", (void*)b3dInitializeRasterizerState},
{(void*)_m, "b3dInitPrimitiveObject\000\001", (void*)b3dInitPrimitiveObject},
{(void*)_m, "b3dInplaceHouseHolderInvert\000\000", (void*)b3dInplaceHouseHolderInvert},
{(void*)_m, "b3dLoadIndexArray\000\000", (void*)b3dLoadIndexArray},
Expand Down Expand Up @@ -4272,7 +4231,7 @@ signed char b3dClipPolygonAccessorDepth = 1;
signed char b3dComputeMinIndexZAccessorDepth = 1;
signed char b3dComputeMinZAccessorDepth = 1;
signed char b3dDetermineClipFlagsAccessorDepth = 1;
signed char b3dInitializeRasterizerStateAccessorDepth = 2;
signed char b3dInitializeRasterizerStateAccessorDepth = 1;
signed char b3dInitPrimitiveObjectAccessorDepth = 1;
signed char b3dInplaceHouseHolderInvertAccessorDepth = 0;
signed char b3dLoadIndexArrayAccessorDepth = 0;
Expand Down

0 comments on commit 745fbbf

Please sign in to comment.