PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Your thoughts on runtime code generation/modification...

hi,

i would like to get your comments on the following ideas:

for a 3D renderer with a very flexible API (OpenGL|ES like) i have the problem of creating fast rasterization code. optimal scanline rendering code would have a fixed feature set (say: texturing + gouraud, but no fog and blending). the way i currently solve this problem is to use a python script that creates all kinds of combinations for compile-time function creation. at the moment this means 735 scanline functions, which results in roughly 1MB code size (say a 1MB DLL). by adding more features the code size would grow enormously (e.g. code size would double by adding another independent feature).

obviously an application will only use a very small number of these functions at runtime. so i thought about how to solve this issue at run-time. two solutions came into my mind:

1st: using a PPC native compiler to create necessary code at runtime and dynamically link it to the application.

2nd: self modifying code. write one scanline function that can do all the features. at runtime for each feature combination a copy of this generic function is created and jump-operations are added to skip features that should not be executed.

any comments on these ideas (or other completely different proposals)?

thx,
DANIEL
Statically link ?

I think that evc does not include not used functions.
StephC wrote:
Statically link ?

I think that evc does not include not used functions.


The problem is that they all get used if you put them in a jump table (or any other kind of data structure).

My experience on this is that most games only need 4-8 optimized rendering paths, not all possible combinaisons.

I would myself write a generic render path that can do everything and only optimize the cases that are worth it (i.e. used often). If 90% of your triangles are texture-mapped/Gouraud shading with Z-buffer, then only optimize that path.

Dynamic generation is always interesting but not that useful if you know in advance what paths you need. Of course, dynamic generation could also allow you to optimize for the specific processors (but then again there is only 2 or 3: StrongARM, XScale and the new Samsung).
well, the problem is that for a general implementation of a 3D API like OpenGL (and not a specific game) you don't know in advance which functions are going to be needed by a programmer.

you are right that obviously a small number of functions will be used more often than most others. that's why i allow developers to choose in the python script which functions will be created. at the moment i don't have a fall-back for those functions that were not selected (so the library complains about this instead of doing it at least slowly) but that's going to come.

but i'd love to see a better solution to this...

thx,
DANIEL
drgoldie wrote:
well, the problem is that for a general implementation of a 3D API like OpenGL (and not a specific game) you don't know in advance which functions are going to be needed by a programmer.


At one point I compiled MesaGL and got a 4.5 megs library. At that point, I understood that a full OpenGL implementation was out of the question on the PocketPC.

drgoldie wrote:
but i'd love to see a better solution to this...


May I suggest templates? They only get compiled if you use them...
At one point I compiled MesaGL and got a 4.5 megs library. At that point, I understood that a full OpenGL implementation was out of the question on the PocketPC.

yes, that's true. mesa targets full OpenGL compliance. that's why it's also quite slow (especially on a PDA) and that's also why OpenGL|ES was defined. :)

templates won't help here. to stress the problem again: i want to build a dll that supports ALL possible features at the rasterizer stage. unfortunately that's a lot of combinations if i want each scanline function to support only one feature set. at compile time it's not know if the application programmer will use a feature or not. therefore i have to put them all into the dll (or generate them at runtime)

DANIEL
drgoldie wrote:
templates won't help here. to stress the problem again: i want to build a dll that supports ALL possible features at the rasterizer stage. unfortunately that's a lot of combinations if i want each scanline function to support only one feature set. at compile time it's not know if the application programmer will use a feature or not. therefore i have to put them all into the dll (or generate them at runtime)


Then your only choice is dynamic code generation =)
well, that's were i was at the beginning... :)

so the main question for me is what's the best way to do it. i like the idea of using the native arm compiler which way announced in this forum. it seems to be a clean solution. but then i'd have to try out how good this compiler can optimize code...

does anybody know about a native arm assembler?

how would i link runtime generated code to my application? all i should need is a function pointer. can i simply create binary code and use the entry point as function pointer (i've got to admit, that my machine code experiences are very basic at this level).

thx,
DANIEL
Sergey Chaban · 11 Dec 2003, 16:01 UTC
Daniel,

IMO, using native compiler is completely out of question for PocketPC (considering startup times and memory requirements).
There is native assembler available: http://www.rainer-keuchel.de/wince/armasm.html


You may want to take a look at my codegen library for ARM.
It's available from several places, but contact me directly (serge at wildwestsoftware . com) for the latest version.

Here is a quick sample to give you some idea how it works. All in all it's something like run-time assembler.
This generates some fixed-point vector math functions (given fracBits number) then executes generated code:

Code:

struct VecMath {
arminstr_t* code;
void (*Add)(Vec3D* v1, Vec3D* v2, Vec3D* vres);
void (*Sub)(Vec3D* v1, Vec3D* v2, Vec3D* vres);
void (*Dot)(Vec3D* v1, Vec3D* v2, fix_t* d);
// ... other functions
};

arminstr_t* GenVec3AddSub(arminstr_t* p, int fracBits, int add) {
// r0: v1
// r1: v2
// r2: vres
ARM_PUSH4(p, ARMREG_R4, ARMREG_R5, ARMREG_R6, ARMREG_LR);
ARM_LDMIA(p, ARMREG_R0, (1 << ARMREG_R4) | (1 << ARMREG_R5) | (1 << ARMREG_R6)); // v1
ARM_LDMIA(p, ARMREG_R1, (1 << ARMREG_R3) | (1 << ARMREG_R12) | (1 << ARMREG_LR)); // v2
if (add) {
ARM_ADD_REG_REG(p, ARMREG_R4, ARMREG_R4, ARMREG_R3);
ARM_ADD_REG_REG(p, ARMREG_R5, ARMREG_R5, ARMREG_R12);
ARM_ADD_REG_REG(p, ARMREG_R6, ARMREG_R6, ARMREG_LR);
} else {
ARM_SUB_REG_REG(p, ARMREG_R4, ARMREG_R4, ARMREG_R3);
ARM_SUB_REG_REG(p, ARMREG_R5, ARMREG_R5, ARMREG_R12);
ARM_SUB_REG_REG(p, ARMREG_R6, ARMREG_R6, ARMREG_LR);
}
ARM_STMIA(p, ARMREG_R2, (1 << ARMREG_R4) | (1 << ARMREG_R5) | (1 << ARMREG_R6)); // vres
ARM_POP4(p, ARMREG_R4, ARMREG_R5, ARMREG_R6, ARMREG_PC);
return p;
}

arminstr_t* GenVec3Dot(arminstr_t* p, int fracBits) {
// r0: v1
// r1: v2
// r2: res
ARM_PUSH5(p, ARMREG_R4, ARMREG_R5, ARMREG_R6, ARMREG_R7, ARMREG_LR);
ARM_LDMIA(p, ARMREG_R0, (1 << ARMREG_R4) | (1 << ARMREG_R5) | (1 << ARMREG_R6)); // v1
ARM_LDMIA(p, ARMREG_R1, (1 << ARMREG_R3) | (1 << ARMREG_R7) | (1 << ARMREG_R12)); // v2
ARM_SMULL(p, ARMREG_LR, ARMREG_R3, ARMREG_R4, ARMREG_R3);
ARM_SMLAL(p, ARMREG_LR, ARMREG_R3, ARMREG_R5, ARMREG_R7);
ARM_SMLAL(p, ARMREG_LR, ARMREG_R3, ARMREG_R6, ARMREG_R12);
ARM_MOV_REG_IMMSHIFT(p, ARMREG_R3, ARMREG_R3, ARMSHIFT_LSL, (32 - fracBits));
ARM_ORR_REG_IMMSHIFT(p, ARMREG_R3, ARMREG_R3, ARMREG_LR, ARMSHIFT_LSR, fracBits);
ARM_STR_IMM(p, ARMREG_R3, ARMREG_R2, 0);
ARM_POP5(p, ARMREG_R4, ARMREG_R5, ARMREG_R6, ARMREG_R7, ARMREG_PC);
return p;
}


VecMath* GenVecMath(int fracBits) {
VecMath* vecMath = new VecMath();
arminstr_t* p = (arminstr_t*)AllocCodeBuff(1024);
vecMath->code = p;

vecMath->Add = (void (*)(Vec3D*, Vec3D*, Vec3D*))p;
p = GenVec3AddSub(p, fracBits, 1);

vecMath->Sub = (void (*)(Vec3D*, Vec3D*, Vec3D*))p;
p = GenVec3AddSub(p, fracBits, 0);

vecMath->Dot = (void (*)(Vec3D*, Vec3D*, fix_t*))p;
p = GenVec3Dot(p, fracBits);

// ...

return vecMath;
}

// ...
VecMath* vec8Math = GenVecMath(8);
Vec3D v1;
Vec3D v2;
Vec3D v3;
fix_t d = 0;

v1.x = 1 << 8;
v1.y = 2 << 8;
v1.z = 3 << 8;

v2.x = 4 << 8;
v2.y = 5 << 8;
v2.z = 6 << 8;

vec8Math->Add(&v1, &v2, &v3);
vec8Math->Dot(&v1, &v2, &d);
// ...



Another option is to construct your code from pre-assembled code fragments (rather than from individual instructions). I think this is how Pixomatic renderer is implemented.

SC
hey thanks, this looks VERY interesting.

i have to admit that i first want to try out the Mesa way: filling a pixel-buffer for each "feature" (such as texture, shading, etc...) and finally combine them in the way as required.

i used to think that this would be very slow, but the buffers should fit nicely into the cache and other than i used to believe, an early z-test can be done efficiently (due to the fact that pixels will not switch frequently between visible and unvisible).

if it should turn out, that this solution is ways slower than what i have right now i'll definitely try out the assembler stuff. what's keeping me away from it right now is the fact that i'll have to learn ARM assembler...

thanks,
DANIEL
drgoldie wrote:
what's keeping me away from it right now is the fact that i'll have to learn ARM assembler...


ARM assembler is quite easy. This is a RISC architecture and it just doesn't get better then this.

When learning a new assembler, opcodes are always a little scary.
Thierry wrote:
ARM assembler is quite easy. This is a RISC architecture and it just doesn't get better then this.

When learning a new assembler, opcodes are always a little scary.

In line with this subject, does anybody out here know the best way to flush the instruction cache on an ARM processor?

Thanks,
Martin
I'd use CacheSync, but you might want to look at this.
hm · 07 Mar 2004, 22:42 UTC — coredll references
Digby wrote:I'd use CacheSync, but you might want to look at this.

CacheSync works. As I am packaging up the project for SourceForge http://ogl-es.sourceforge.net, I had to realize that I could find the declarations for CacheSync and similar kernel functions only in the Platform builder sources, but not as part of the regular PPC SDK. The import libs are included though. Hence my question: Is just my install corrupted, or are the kernel function includes indeed missing in the regular PPC SDK install? To keep things self-contained, I opted for copying the declarations I need into files that are part of my project but I really don't like this approach.

- HM
To my knowledge they're not part of the standard embedded tools install - they're only in Platform Builder.
refractor wrote:To my knowledge they're not part of the standard embedded tools install - they're only in Platform Builder.

I kept the definitions in line. Anyway, the code (i.e. runtime rasterizer code generation for OpenGL ES-type API) is now downloadable from SourceForge http://sourceforge.net/projects/ogl-es/.

Before more people ask: Yes, using PocketHAL support will be available soon, and Thierry was kind enough to allow the inclusion of PocketHAL in the next release.

- HM
I saw a message on this OpenGL ES compliant library on the Khronos.org site earlier this week. It also claims to use self-generating code for the inner loops of its rasterizer.

Click here