Thanks for the replies. Yes I should have included what the COLOR class is, it is a very basic 4 byte structure and looks like this:
Code:
class COLOR
{
public:
BYTE b;
BYTE g;
BYTE r;
BYTE a;
COLOR() {r = g = b = a = 0;}
COLOR(int rr, int gg, int bb, int aa)
{
r = rr;
g = gg;
b = bb;
a = aa;
}
};
I create the color buffers in one contiguous block of memory like this:
Code:
colors1 = (COLOR *)malloc(4 * (width*height));
The loops cannot be collapsed into one because one of the buffers (Direct3D texture) is flipped in the y axis and I usually only update part of the whole texture.
I'll provide a more complete example of what I'm doing, I'm blending different layers into a Direct3D texture. I have set it to use pointer arithmetic now:
Code:
for (int i=0;i<numLayers;i++)
{
//Setup info about layer rect and properties here
for (int y=startY;y<endY;y++)
{
int yval = layerWidth * (y - rectTop);
int yvalD3D = width * (heightMinusOne - y);
COLOR* col = layerColors + (yval + (startX - rectLeft));
COLOR* colD3D = colors + (yvalD3D + startX);
if (i) //Not background layer
{
int blend;
COLOR24 blendedCol;
BYTE* source;
BYTE* dest;
for (int x=startX;x<endX;x++, col++, colD3D++)
{
if (!col->a) continue;
blend = col->a * opacity;
if (blendMode) blendedCol = BlendColors(colD3D,col,blendMode);
else blendedCol = COLOR24(col->r,col->g,col->b);
if (blend == 255)
{
colD3D->r = blendedCol.r;
colD3D->g = blendedCol.g;
colD3D->b = blendedCol.b;
}
else
{
source = blendingTable[blend].row;
dest = blendingTable[255 - blend].row;
colD3D->r = source[blendedCol.r] + dest[colD3D->r];
colD3D->g = source[blendedCol.g] + dest[colD3D->g];
colD3D->b = source[blendedCol.b] + dest[colD3D->b];
}
}
}
else //Background layer
{
for (int x=startX;x<endX;x++, col++, colD3D++)
{
*colD3D = *col;
}
}
}
}
The test time I posted is simply for updating the background layer though so as you can see there are not many other calculations in the loops then.
I'm not sure if things can be optimized, would using assembly for some parts be benefitial? I don't know much about it but I'm willing to learn if there could be significant gains.