shithub: tinygl

Download patch

ref: e87bfcde4f340cdcc2e0ff2fbb4153400af945ba
parent: 28903eadf9de092b57a1f8d9ed18cc749596e472
author: David <gek@katherine>
date: Wed Mar 10 11:54:08 EST 2021

Automatic commit.

--- a/README.md
+++ b/README.md
@@ -56,6 +56,7 @@
 
 
 
+NOTE: There are graphical artifacts visible in these gifs which have been corrected in this version of the library.
 
 Without Polygon Stipple:
 
@@ -298,7 +299,15 @@
 
 Every call of the function pointer is run by a separate thread.
 
-Compile the library with -fopenmp to see them in action. They are used in the texture demo, make sure to add the argument `-pp`
+* glCopyTexImage2D
+
+Every scan line is copied by a separate thread.
+
+* ZBCopyBuffer
+
+Every scan line is copied by a separate thread.
+
+Compile the library with -fopenmp to see them in action (default). They are used in the texture demo, make sure to add the argument `-pp`
 
 You do not need a multithreaded processor to use TinyGL!
 
--- a/include/zfeatures.h
+++ b/include/zfeatures.h
@@ -62,7 +62,15 @@
 #define TGL_COLOR_MASK 0x00ffffff
 //^ mask to check for while drawing/copying.
 
+//MULTITHREADING OPTIONS, only applicable if compiled with -fopenmp
 
+//Enable DrawPixels to offload the rendering of individual scanlines to separate threads.
+#define TGL_FEATURE_MULTITHREADED_DRAWPIXELS	1
+//Enable multithreading glCopyTexImage2D
+#define TGL_FEATURE_MULTITHREADED_COPY_TEXIMAGE_2D 1
+//Enable multithreading the ZB_CopyBuffer operation.
+#define TGL_FEATURE_MULTITHREADED_ZB_COPYBUFFER 1
+
 //DO NOT TURN THESE ON, I don't maintain them and I actively #error them out.
 #define TGL_FEATURE_8_BITS         0
 #define TGL_FEATURE_24_BITS        0
@@ -94,3 +102,5 @@
 
 #endif 
 /* _tgl_features_h_ */
+
+#define TINYGL_VERSION 0.8
--- a/src/get.c
+++ b/src/get.c
@@ -1,11 +1,11 @@
 #include "msghandling.h"
 #include "zgl.h"
-#define TINYGL_VERSION 0.8
 
+
 #define xstr(s) str(s)
 #define str(s) #s
 
-/*
+//Including this 
 const GLubyte* license_string = (const GLubyte*)""
 "Copyright notice:\n"
 "\n"
@@ -29,7 +29,6 @@
 "\n"
 "If you redistribute modified sources, I would appreciate that you\n"
 "include in the files history information documenting your changes.";
-*/
 
 const GLubyte* vendor_string = (const GLubyte*)"Fabrice Bellard, Gek, and the C-Chads";
 const GLubyte* renderer_string = (const GLubyte*)"TinyGL";
@@ -47,7 +46,7 @@
 xstr(__TINYC__)
 #endif
 #ifdef _MSVC_VER
-"Compiled using the worst compiler on earth, M$VC. Fuck you!"
+"Compiled using the worst compiler on earth, M$VC. "
 #endif
 #ifdef __clang__
 "Compiled Using Clang "
@@ -128,6 +127,24 @@
 #if TGL_FEATURE_TINYGL_RUNTIME_COMPAT_TEST == 1
 "TGL_FEATURE_TINYGL_RUNTIME_COMPAT_TEST "
 #endif
+#if defined(_OPENMP)
+"TGL_FEATURE_MULTI_THREADED "
+
+#if TGL_FEATURE_MULTITHREADED_DRAWPIXELS == 1
+"TGL_FEATURE_MULTITHREADED_DRAWPIXELS "
+#endif
+
+#if TGL_FEATURE_MULTITHREADED_COPY_TEXIMAGE_2D == 1
+"TGL_FEATURE_MULTITHREADED_COPY_TEXIMAGE_2D "
+#endif
+
+#if TGL_FEATURE_MULTITHREADED_ZB_COPYBUFFER == 1
+"TGL_FEATURE_MULTITHREADED_ZB_COPYBUFFER "
+#endif
+
+#else
+"TGL_FEATURE_SINGLE_THREADED "
+#endif
 "TGL_BUFFER_EXT "
 "TGL_FEEDBACK "
 "TGL_SELECT "
@@ -138,7 +155,7 @@
 		case GL_RENDERER: return renderer_string;
 		case GL_VERSION: return version_string;
 		case GL_EXTENSIONS: return extensions_string;
-		/*case GL_LICENSE: return license_string;*/
+		case GL_LICENSE: return license_string;
 	}
 	return NULL;
 }
@@ -241,6 +258,7 @@
 		params[0] = 0;
 		break;
 	case GL_TEXTURE_2D:
+	case GL_TEXTURE_1D:
 		params[0] = c->texture_2d_enabled;
 		break;
 	case GL_POLYGON_STIPPLE:
@@ -273,13 +291,13 @@
 		break;
 	case GL_LINE_STIPPLE_REPEAT:
 		*params = 1;
-	break;
+		break;
 	case GL_LINE_STIPPLE:
 		*params = 0;
-	break;
+		break;
 	case GL_LINE_STIPPLE_PATTERN:
 		*params = (GLushort)(~0);
-	break;
+		break;
 	case GL_NORMALIZE:
 		params[0] = c->normalize_enabled;
 		break;
@@ -311,7 +329,7 @@
 	case GL_RED_BIAS:
 	case GL_GREEN_BIAS:
 	case GL_ALPHA_BIAS:
-	case GL_CLIP_PLANE0: //TODO
+	case GL_CLIP_PLANE0:
 	case GL_CLIP_PLANE1:
 	case GL_CLIP_PLANE2:
 	case GL_CLIP_PLANE3:
@@ -327,7 +345,7 @@
 	case GL_TEXTURE_GEN_S:
 	case GL_TEXTURE_GEN_R:
 	case GL_TEXTURE_GEN_Q:
-	case GL_TEXTURE_1D: //TODO
+	
 	case GL_STEREO:
 	case GL_PACK_SWAP_BYTES:
 	case GL_PACK_SKIP_ROWS:
--- a/src/texture.c
+++ b/src/texture.c
@@ -242,19 +242,20 @@
 	im->ysize = TGL_FEATURE_TEXTURE_DIM;
 	//sample the buffer.
 	//TODO implement the scaling and stuff that the GL spec says it should have.
+#if TGL_FEATURE_MULTITHREADED_COPY_TEXIMAGE_2D == 1
 	for(GLint j = 0; j < h; j++)
 	for(GLint i = 0; i < w; i++){
-		/*
-		GLfloat dx = (GLfloat)i/(GLfloat)w;
-		GLfloat dy = (GLfloat)j/(GLfloat)h;
-		dx *= TGL_FEATURE_TEXTURE_DIM;
-		dy *= TGL_FEATURE_TEXTURE_DIM;
-		GLuint xdest = (dx<0)?0:((dx>TGL_FEATURE_TEXTURE_DIM-1)?TGL_FEATURE_TEXTURE_DIM-1:dx);
-		GLuint ydest = (dy<0)?0:((dy>TGL_FEATURE_TEXTURE_DIM-1)?TGL_FEATURE_TEXTURE_DIM-1:dy);
-		*/
 		data[i+j*w] = c->zb->pbuf[		((i+x)%(c->zb->xsize))
 									+	((j+y)%(c->zb->ysize))*(c->zb->xsize)];
 	}
+#else
+#pragma omp parallel for
+	for(GLint j = 0; j < h; j++)
+	for(GLint i = 0; i < w; i++){
+		data[i+j*w] = c->zb->pbuf[		((i+x)%(c->zb->xsize))
+									+	((j+y)%(c->zb->ysize))*(c->zb->xsize)];
+	}
+#endif
 }
 
 void glopTexImage1D(GLContext* c, GLParam* p){
--- a/src/zbuffer.c
+++ b/src/zbuffer.c
@@ -119,13 +119,11 @@
 #endif
 
 static void ZB_copyBuffer(ZBuffer* zb, void* buf, GLint linesize) {
-	GLubyte* p1;
-	PIXEL* q;
-	GLint y;
-
-	q = zb->pbuf;
-	p1 = buf;
-	for (y = 0; y < zb->ysize; y++) {
+#if TGL_FEATURE_MULTITHREADED_ZB_COPYBUFFER == 1
+#pragma omp parallel for
+	for (GLint y = 0; y < zb->ysize; y++) {
+		PIXEL* q = zb->pbuf + y * zb->xsize;
+		GLubyte* p1 = buf + y * linesize;
 #if TGL_FEATURE_NO_COPY_COLOR == 1
 		for (GLint i = 0; i < zb->xsize; i++) {
 			if ((*(q + i) & TGL_COLOR_MASK) != TGL_NO_COPY_COLOR)
@@ -134,9 +132,25 @@
 #else
 		memcpy(p1, q, linesize);
 #endif
-		p1 += linesize;
-		q = (PIXEL*)((GLbyte*)q + zb->linesize);
+		//p1 += linesize;
+		//q = (PIXEL*)((GLbyte*)q + zb->linesize);
 	}
+#else
+	for (GLint y = 0; y < zb->ysize; y++) {
+		PIXEL* q = zb->pbuf + y * zb->xsize;
+		GLubyte* p1 = buf + y * linesize;
+#if TGL_FEATURE_NO_COPY_COLOR == 1
+		for (GLint i = 0; i < zb->xsize; i++) {
+			if ((*(q + i) & TGL_COLOR_MASK) != TGL_NO_COPY_COLOR)
+				*(((PIXEL*)p1) + i) = *(q + i);
+		}
+#else
+		memcpy(p1, q, linesize);
+#endif
+		//p1 += linesize;
+		//q = (PIXEL*)((GLbyte*)q + zb->linesize);
+	}
+#endif
 }
 
 #if TGL_FEATURE_RENDER_BITS == 16
--- a/src/zraster.c
+++ b/src/zraster.c
@@ -149,7 +149,10 @@
 		);
 		return;
 	}
+
 // Works.
+#if TGL_FEATURE_MULTITHREADED_DRAWPIXELS == 1
+
 #pragma omp parallel for
 	for(GLint sy = 0; sy < h; sy++)
 	for(GLint sx = 0; sx < w; sx++)
@@ -160,8 +163,43 @@
 		rastoffset.v[1] = rastpos.v[1] - ((GLfloat)(h-sy) * pzoomy);
 		rastoffset.v[2] = rastoffset.v[0] + pzoomx;
 		rastoffset.v[3] = rastoffset.v[1] - pzoomy;
+		
+		for(GLint ty = rastoffset.v[1]; (GLfloat)ty > rastoffset.v[3];ty--)
 		for(GLint tx = rastoffset.v[0]; (GLfloat)tx < rastoffset.v[2];tx++)
+			if(CLIPTEST(tx,ty,tw,th)){
+			GLushort* pz = zbuf + (ty * tw + tx);
+
+				if(ZCMP(zz,*pz)){
+
+#if TGL_FEATURE_BLEND == 1
+#if TGL_FEATURE_BLEND_DRAW_PIXELS == 1
+					if(!zbeb)
+						pbuf[tx+ty*tw] = col;
+					else
+						TGL_BLEND_FUNC(col, pbuf[tx+ty*tw])
+#else
+					pbuf[tx+ty*tw] = col;
+#endif
+#else
+					pbuf[tx+ty*tw] = col;
+#endif
+					if(zbdw) *pz = zz;
+				}
+			}
+	}
+#else
+	for(GLint sy = 0; sy < h; sy++)
+	for(GLint sx = 0; sx < w; sx++)
+	{
+		PIXEL col = d[sy*w+sx];
+		V4 rastoffset;
+		rastoffset.v[0] = rastpos.v[0] +  (GLfloat)sx * pzoomx;
+		rastoffset.v[1] = rastpos.v[1] - ((GLfloat)(h-sy) * pzoomy);
+		rastoffset.v[2] = rastoffset.v[0] + pzoomx;
+		rastoffset.v[3] = rastoffset.v[1] - pzoomy;
+		
 		for(GLint ty = rastoffset.v[1]; (GLfloat)ty > rastoffset.v[3];ty--)
+		for(GLint tx = rastoffset.v[0]; (GLfloat)tx < rastoffset.v[2];tx++)
 			if(CLIPTEST(tx,ty,tw,th)){
 			GLushort* pz = zbuf + (ty * tw + tx);
 
@@ -183,6 +221,7 @@
 				}
 			}
 	}
+#endif
 }