~melchizedek6809/WolkenWelten

ff4cacb731b1d23a372b0a45eb8e3f76a8fc7b08 — Ben (X13) 2 years ago 9c5e2fd ispc
WIP: Sunlight calculations are now also done via ISPC
3 files changed, 44 insertions(+), 16 deletions(-)

M common/src/game/light.c
M common/src/game/lightFast.h
M common/src/game/lightFast.ispc
M common/src/game/light.c => common/src/game/light.c +14 -14
@@ 24,7 24,7 @@
#include <string.h>


static void lightSunlightChunk(u8 out[48][48][48], const u8 blockData[16][16][16], u8 curLight[16][16], const u8 blockLight[256], const int x, const int y, const int z, const u8 sunlight){
void lightSunlightChunk(u8 out[48][48][48], const u8 blockData[16][16][16], u8 curLight[16][16], const u8 blockLight[256], const int x, const int y, const int z, const u8 sunlight){
	for(int cy=15;cy>=0;cy--){
	for(int cx=0;cx<16;cx++){
	for(int cz=0;cz<16;cz++){


@@ 41,7 41,7 @@ static void lightSunlightChunk(u8 out[48][48][48], const u8 blockData[16][16][16
	}
}

static void lightSunlightAir(u8 out[48][48][48], u8 curLight[16][16], const int x, const int y, const int z, const u8 sunlight){
void lightSunlightAir(u8 out[48][48][48], u8 curLight[16][16], const int x, const int y, const int z, const u8 sunlight){
	for(int cy=15;cy>=0;cy--){
	for(int cx=0;cx<16;cx++){
	for(int cz=0;cz<16;cz++){


@@ 52,15 52,8 @@ static void lightSunlightAir(u8 out[48][48][48], u8 curLight[16][16], const int 
	}
}

static void lightSunlight(u8 out[48][48][48],const chunkOverlay *block[3][3][3]){
static void lightSunlight(u8 out[48][48][48],const chunkOverlay *block[3][3][3], const u8 blockLight[256], const u8 sunlight){
	PROFILE_START();

	u8 blockLight[256];
	for(int b=0;b<256;b++){
		blockLight[b] = blockTypeGetLightEmission(b);
	}
	const u8 sunlight = gtimeGetBlockBrightness(gtimeGetTimeOfDay());

	for(int x = 0;x < 3; x++){
	for(int z = 0;z < 3; z++){
	u8 curLight[16][16];


@@ 68,9 61,9 @@ static void lightSunlight(u8 out[48][48][48],const chunkOverlay *block[3][3][3])
	for(int y = 2;y >= 0; y--){
		const chunkOverlay *cur = block[x][y][z];
		if(cur){
			lightSunlightChunk(out, cur->data, curLight, blockLight, x*16, y*16, z*16, sunlight);
			lightSunlightChunkISPC(out, cur->data, curLight, blockLight, x*16, y*16, z*16, sunlight);
		}else{
			lightSunlightAir(out, curLight, x*16, y*16, z*16, sunlight);
			lightSunlightAirISPC(out, curLight, x*16, y*16, z*16, sunlight);
		}
	}
	}


@@ 80,7 73,7 @@ static void lightSunlight(u8 out[48][48][48],const chunkOverlay *block[3][3][3])

static void lightBlurFast(u8 buf[48][48][48]){
	PROFILE_START();
	ispcLightBlur(buf);
	lightBlurISPC(buf);
	PROFILE_STOP();
}



@@ 94,8 87,15 @@ static void lightOut(u8 in[48][48][48], chunkOverlay *out){

void lightTick(chunkOverlay *light, const chunkOverlay *block[3][3][3]){
	static u8 lightBuffer[48][48][48];
	u8 blockLight[256];
	for(int b=0;b<256;b++){
		blockLight[b] = blockTypeGetLightEmission(b);
	}
	const u8 sunlight = gtimeGetBlockBrightness(gtimeGetTimeOfDay());
	(void)lightSunlight;
	PROFILE_START();
	lightSunlight(lightBuffer, block);

	lightSunlight(lightBuffer, block, blockLight, sunlight);
	lightBlurFast(lightBuffer);
	lightOut(lightBuffer,light);
	PROFILE_STOP();

M common/src/game/lightFast.h => common/src/game/lightFast.h +3 -1
@@ 1,3 1,5 @@
#pragma once

void ispcLightBlur(u8 out[48][48][48]);
void lightSunlightAirISPC(u8 out[48][48][48], u8 curLight[16][16], const int x, const int y, const int z, const u8 sunlight);
void lightSunlightChunkISPC(u8 out[48][48][48], const u8 blockData[16][16][16], u8 curLight[16][16], const u8 blockLight[256], const int x, const int y, const int z, const u8 sunlight);
void lightBlurISPC(u8 out[48][48][48]);

M common/src/game/lightFast.ispc => common/src/game/lightFast.ispc +27 -1
@@ 4,6 4,32 @@
 * hand written assembly.
 */

export void lightSunlightChunkISPC(uniform int8 out[48][48][48],uniform const int8 blockData[16][16][16], uniform int8 curLight[16][16], uniform const int8 blockLight[256], uniform const int x, uniform const int y, uniform const int z, uniform const int8 sunlight){
	for(uniform int cy=15;cy>=0;cy--){
	for(uniform int cx=0;cx<16;cx++){
	for(uniform int cz=0;cz<16;cz++){
		uniform const int8 b = blockData[cx][cy][cz];
		if(b){
			curLight[cx][cz] = 0;
			out[x+cx][y+cy][z+cz] = blockLight[b];
		}else{
			curLight[cx][cz] = min(sunlight, curLight[cx][cz]+2);
			out[x+cx][y+cy][z+cz] = curLight[cx][cz];
		}
	}
	}
	}
}

export void lightSunlightAirISPC(uniform int8 out[48][48][48],uniform int8 curLight[16][16],uniform const int x, uniform const int y, uniform const int z, uniform const int8 sunlight){
	for(uniform int cy=15;cy>=0;cy--){
	foreach(cx = 0 ... 16, cz = 0 ... 16){
		curLight[cx][cz] = min(sunlight, curLight[cx][cz]+2);
		out[cx+x][cy+y][cz+z] = curLight[cx][cz];
	}
	}
}

static void lightBlurZ(uniform int8 out[48][48][48]){
	for(uniform int x=0;x < 48;x++){
	for(uniform int y=0;y < 48;y++){


@@ 64,7 90,7 @@ static void lightBlurX(uniform int8 out[48][48][48]){
	}
}

export void ispcLightBlur(uniform int8 out[48][48][48]){
export void lightBlurISPC(uniform int8 out[48][48][48]){
	lightBlurX(out);
	lightBlurZ(out);
	lightBlurY(out);