shithub: tinygl

Download patch

ref: e71df3beac500cfe5e974c6c1f33bd4883989fdc
parent: 3d80174a853f38ae9501774ae9946cd87d520c36
parent: b8a8adb6da85cd4c79f5c47ccee728fa03cf09a0
author: DMHSW <6974902+gek169@users.noreply.github.com>
date: Mon Sep 6 15:55:52 EDT 2021

Merge pull request #13 from ccawley2011/openmp

Only use OpenMP when _OPENMP is defined, and enable OpenMP in CMake builds

--- a/SDL_Examples/CMakeLists.txt
+++ b/SDL_Examples/CMakeLists.txt
@@ -1,4 +1,5 @@
 find_package(SDL QUIET)
+find_package(OpenMP QUIET)
 
 if(SDL_FOUND)
 
@@ -14,7 +15,7 @@
     foreach(DEMO ${demo_names})
       set(DEMO_NAME "sdl_${DEMO}")
       add_executable(${DEMO_NAME} ${DEMO}.c)
-      target_link_libraries(${DEMO_NAME} ${TINYGL_LIB} ${M_LIBRARY} ${SDL_LIBRARY})
+      target_link_libraries(${DEMO_NAME} PRIVATE ${TINYGL_LIB} ${M_LIBRARY} ${SDL_LIBRARY})
       target_include_directories(${DEMO_NAME} PUBLIC ${SDL_INCLUDE_DIR})
       if(NOT MSVC)
         target_compile_options(${DEMO_NAME} PRIVATE -O3 -DNDEBUG -pedantic -Wall)
@@ -22,6 +23,9 @@
           target_compile_options(tinygl PRIVATE -march=native)
         endif(NOT CMAKE_CROSSCOMPILING)
       endif(NOT MSVC)
+      if(OPENMP_C_FOUND)
+        target_link_libraries(${DEMO_NAME} PRIVATE OpenMP::OpenMP_C)
+      endif(OPENMP_C_FOUND)
     endforeach()
 
     set(TINYGL_RESOURCES
--- a/include-demo/3dMath.h
+++ b/include-demo/3dMath.h
@@ -320,7 +320,9 @@
 }
 static inline mat4 multm4( mat4 a,  mat4 b){
 	mat4 ret;
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 	for(int i = 0; i < 4; i++)
 	for(int j = 0; j < 4; j++)
 		ret.d[i*4 + j] = dotv4( /*j is the ROW of the target, i is the COLUMN.*/
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -28,6 +28,8 @@
   ztriangle.c
   )
 
+find_package(OpenMP)
+
 if(TINYGL_BUILD_SHARED)
   add_library(tinygl SHARED ${tinygl_srcs})
   target_include_directories(tinygl PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include)
@@ -41,6 +43,9 @@
       target_compile_options(tinygl PRIVATE -march=native)
     endif(NOT CMAKE_CROSSCOMPILING)
   endif(NOT MSVC)
+  if(OPENMP_C_FOUND)
+    target_link_libraries(tinygl PUBLIC OpenMP::OpenMP_C)
+  endif(OPENMP_C_FOUND)
 endif(TINYGL_BUILD_SHARED)
 
 if(TINYGL_BUILD_STATIC)
@@ -56,6 +61,9 @@
       target_compile_options(tinygl-static PRIVATE -march=native)
     endif(NOT CMAKE_CROSSCOMPILING)
   endif(NOT MSVC)
+  if(OPENMP_C_FOUND)
+    target_link_libraries(tinygl-static PUBLIC OpenMP::OpenMP_C)
+  endif(OPENMP_C_FOUND)
 endif(TINYGL_BUILD_STATIC)
 
 # Local Variables:
--- a/src/clip.c
+++ b/src/clip.c
@@ -113,7 +113,9 @@
 	q->pc.Y = p0->pc.Y + (p1->pc.Y - p0->pc.Y) * t;
 	q->pc.Z = p0->pc.Z + (p1->pc.Z - p0->pc.Z) * t;
 	q->pc.W = p0->pc.W + (p1->pc.W - p0->pc.W) * t;
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 	for (i = 0; i < 3; i++)
 		q->color.v[i] = p0->color.v[i] + (p1->color.v[i] - p0->color.v[i]) * t;
 }
--- a/src/texture.c
+++ b/src/texture.c
@@ -232,7 +232,9 @@
 	im->ysize = TGL_FEATURE_TEXTURE_DIM;
 	/* TODO implement the scaling and stuff that the GL spec says it should have.*/
 #if TGL_FEATURE_MULTITHREADED_COPY_TEXIMAGE_2D == 1
+#ifdef _OPENMP
 #pragma omp parallel for
+#endif
 	for (j = 0; j < h; j++)
 		for (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)];
--- a/src/zbuffer.c
+++ b/src/zbuffer.c
@@ -117,7 +117,9 @@
 static void ZB_copyBuffer(ZBuffer* zb, void* buf, GLint linesize) {
 	GLint y, i;
 #if TGL_FEATURE_MULTITHREADED_ZB_COPYBUFFER == 1
+#ifdef _OPENMP
 #pragma omp parallel for
+#endif
 	for (y = 0; y < zb->ysize; y++) {
 		PIXEL* q;
 		GLubyte* p1;
--- a/src/zmath.c
+++ b/src/zmath.c
@@ -11,7 +11,9 @@
 void gl_M4_Id(M4* a) {
 	/*
 		GLint i, j;
+	#ifdef _OPENMP
 	#pragma omp simd collapse(2)
+	#endif
 		for (i = 0; i < 4; i++)
 			for (j = 0; j < 4; j++)
 				if (i == j)
@@ -53,7 +55,9 @@
 void gl_M4_Mul(M4* c, M4* a, M4* b) {
 	GLint i, j, k;
 	GLfloat s;
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 	for (i = 0; i < 4; i++)
 		for (j = 0; j < 4; j++) {
 			s = 0.0;
@@ -72,7 +76,9 @@
 	/*memcpy(&a, c, 16*sizeof(GLfloat));
 	 */
 	a = *c;
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 	for (i = 0; i < 4; i++)
 		for (j = 0; j < 4; j++) {
 			s = 0.0;
@@ -136,7 +142,9 @@
 void gl_M4_InvOrtho(M4* a, M4 b) {
 	GLint i, j;
 	GLfloat s;
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 	for (i = 0; i < 3; i++)
 		for (j = 0; j < 3; j++)
 			a->m[i][j] = b.m[j][i];
@@ -147,7 +155,9 @@
 
 	for (i = 0; i < 3; i++) {
 		s = 0;
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 		for (j = 0; j < 3; j++)
 			s -= b.m[j][i] * b.m[j][3];
 		a->m[i][3] = s;
@@ -162,7 +172,9 @@
 	GLfloat max, tmp, t;
 
 	/*  */
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 	for (i = 0; i < n * n; i++)
 		r[i] = 0;
 	for (i = 0; i < n; i++)
@@ -184,7 +196,9 @@
 
 		/* permutation des lignes j et k */
 		if (k != j) {
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 			for (i = 0; i < n; i++) {
 				tmp = m[j * n + i];
 				m[j * n + i] = m[k * n + i];
@@ -198,7 +212,9 @@
 
 		/* multiplication de la ligne j par 1/max */
 		max = 1 / max;
+#ifdef _OPENMP
 #pragma omp simd
+#endif
 		for (i = 0; i < n; i++) {
 			m[j * n + i] *= max;
 			r[j * n + i] *= max;
--- a/src/zpostprocess.c
+++ b/src/zpostprocess.c
@@ -5,7 +5,9 @@
 void glPostProcess(GLuint (*postprocess)(GLint x, GLint y, GLuint pixel, GLushort z)) {
 	GLint i, j;
 	GLContext* c = gl_get_context();
+#ifdef _OPENMP
 #pragma omp parallel for collapse(2)
+#endif
 	for (j = 0; j < c->zb->ysize; j++)
 		for (i = 0; i < c->zb->xsize; i++)
 			c->zb->pbuf[i + j * (c->zb->xsize)] = postprocess(i, j, c->zb->pbuf[i + j * (c->zb->xsize)], c->zb->zbuf[i + j * (c->zb->xsize)]);
--- a/src/zraster.c
+++ b/src/zraster.c
@@ -141,7 +141,9 @@
 
 #if TGL_FEATURE_MULTITHREADED_DRAWPIXELS == 1
 
+#ifdef _OPENMP
 #pragma omp parallel for
+#endif
 	for (sy = 0; sy < h; sy++)
 		for (sx = 0; sx < w; sx++) {
 			PIXEL col = d[sy * w + sx];