I am writing a Software program 3D Renderer in C many months after I first initially wrote it in Python. It’s made utilizing SDL2. My engine at present has depth shading, perspective appropriate texturing and a few very nice efficiency (330 FPS at 1920×1080 on my Ryzen 5 5500, or a body time of about 3ms.). Nevertheless, I made a decision so as to add in a settings characteristic to my engine that might permit the person to alter the decision with out having to recompile this system. This is able to find yourself with me having to try to determine a serious bug which ended up in large failure – a bug that solely occured every time I set the display decision from an array throughout runtime.
I’ve completed my finest to repair the issue, and have not had any success in anyway. I’ve nonetheless observed that if the variables are identified throughout compilation, my efficiency doesn’t undergo, even when I name the perform for it at runtime. If the values are unknown and I modify them throughout runtime, my efficiency is lower down by virtually half.
I’ve requested in just a few locations earlier than and I’ve had no success with the ideas given to me. I don’t wish to use a profiler as a result of there’s nothing to profile, I do know the place the issue is however I have no idea how one can repair that downside – one thing a profiler won’t straight inform me the reply for. I at present suspect that the compiler is upset with the actual fact my variables usually are not constants even when modified as soon as and don’t apply the suitable optimizations. I’ve tried writing a few of my raster features in ASM however that has failed.
My Raster Capabilities. I apologise if a few of it’s sloppy.
static inline void draw_wall(float sx0, float sx1,
float sy0, float sy1, float sy2, float sy3, Uint16 * prohibit pixels,
portalCull portalBounds, int * prohibit ceilingLut, int * prohibit floorLut, int flat, uint16_t * prohibit w_pixels, float t0, float t1, float wy0, float wy1, float size, float top) {
float dx = (sx1 > sx0) ? (sx1 - sx0) : 1.0f;
int startX = clamp((int)sx0, portalBounds.x0, portalBounds.x1);
int endX = clamp((int)sx1, portalBounds.x0, portalBounds.x1);
float invDx = 1.0f / dx;
float invCullDx = 1.0f / portalBounds.dxCull;
float z0 = 1/wy0;
float z1 = 1/wy1;
float sf = size/4;
float tLeft = (1-t0) * 0 + t0*sf;
float tRight = (1-t1) * 0 + t1*sf;
float tLZ0 = tLeft/z0;
float tRZ1 = tRight/z1;
float wz0 = wy0/z0;
float wz1 = wy1/z1;
float wh = top/4;
float TEXTURE_DETAIL = ((1 << 6)*wh);
for (int x = startX; x < endX; x++) {
float t = (x - sx0) * invDx;
float v = (1-t);
float st = (x - portalBounds.x0) * invCullDx;
float wS = (1-v) * wy0 + v*wy1;
float dS = ((1-v) * wz1 + v*wz0);
int_fast16_t yTop = (int)((1.0f - t) * sy0 + t * sy1);
int_fast16_t yBottom = (int)((1.0f - t) * sy2 + t * sy3);
int_fast16_t tTop = yTop;
int_fast16_t tBottom = yBottom;
int_fast16_t clampY0 = (int_fast16_t)((1.0f - st) * portalBounds.y0 + st * portalBounds.y1);
int_fast16_t clampY1 = (int_fast16_t)((1.0f - st) * portalBounds.y2 + st * portalBounds.y3);
int_fast16_t U __attribute__ ((aligned (4))) = (int_fast16_t)(((1-v) * tLZ0 + v*tRZ1) / wS * (1<<6)) & 0x3F;
yTop = clamp(yTop, clampY0, clampY1);
yBottom = clamp(yBottom, clampY0, clampY1);
int_fast16_t clampSY0 = clamp(yTop, 1, SH1);
int_fast16_t clampSY1 = clamp(yBottom, 1, SH1);
int row = clampSY0 * SW + x;
if (flat == 0) {
ceilingLut[x] = clampSY0;
floorLut[x] = clampSY1;
} else if (flat == 1) {
ceilingLut[x] = clampSY0;
} else if (flat == 2) {
floorLut[x] = clampSY1;
}
float dxY = (tBottom - tTop);
float idxY = (1/dxY) * TEXTURE_DETAIL;
int_fast16_t shade = (int_fast16_t)(1.0f + dS * 0.0025f);
//draw_wall_Y((lengthy lengthy)&pixels[row], SW, clampSY0, clampSY1, tTop, idxY, shade, U, (lengthy lengthy) &w_pixels);
uint16_t *base = &w_pixels[U];
for (int y = clampSY0; y < clampSY1; y++) b;
row += SW;
}
};
static inline void draw_flat(Uint16 *prohibit pixels, int *prohibit lut, int flat, portalCull portalBounds, float elevation, float fov, float yaw, float f, float cx, float cy, uint16_t *prohibit t_pixels) {
int X0 = clamp(clamp((int)0, portalBounds.x0, portalBounds.x1), 1, SW1);
int X1 = clamp(clamp((int)SW1, portalBounds.x0, portalBounds.x1), 1, SW1);
float radFOV = (float)fov*PI/180.0;
float halfRFOV = radFOV/2.0;
float radYaw = (float)((int)yaw)*PI/180.0;
float elevationFactor = elevation * f;
swap (flat) (g << 5)
}
break;
case 2:
for (int x=X0; x> 11) & 0x1F) >> shade);
register uint8_t g = (((baseColour >> 5) & 0x3F) >> shade);
register uint8_t b = ((baseColour & 0x1F) >> shade);
pixels[row] = (r << 11) | (g << 5) | b;
row += SW;
}
}
break;
}
};
Github Repository: https://github.com/GooseyMcGoosington/Scotch-Engine-C