From 3d0cc2e959ddbcc1d80ce6c0a8c4d3664726f8e8 Mon Sep 17 00:00:00 2001
From: havoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Date: Fri, 22 Dec 2006 13:05:38 +0000
Subject: [PATCH] raised MAX_TEXTUREUNITS from 16 to 64 fixed bug with
 backendimageunits/backendarrayunits not being limited to MAX_TEXTUREUNITS
 (caused memory corruption on NVIDIA GeForce 8 series which have 32 texture
 image units), backendunits was already limited properly

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@6663 d7cf8633-e32d-0410-b094-e92efae38249
---
 gl_backend.c | 4 +++-
 gl_backend.h | 3 ++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/gl_backend.c b/gl_backend.c
index 5770fb45..286b5452 100644
--- a/gl_backend.c
+++ b/gl_backend.c
@@ -157,7 +157,7 @@ static void gl_backend_start(void)
 		Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
 	}
 
-	backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
+	backendunits = bound(1, gl_textureunits, MAX_TEXTUREUNITS);
 	backendimageunits = backendunits;
 	backendarrayunits = backendunits;
 	if (gl_support_fragment_shader)
@@ -168,6 +168,8 @@ static void gl_backend_start(void)
 		qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
 		CHECKGLERROR
 		Con_Printf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
+		backendimageunits = bound(1, backendimageunits, MAX_TEXTUREUNITS);
+		backendarrayunits = bound(1, backendarrayunits, MAX_TEXTUREUNITS);
 	}
 	else if (backendunits > 1)
 		Con_Printf("multitexture detected: texture units = %i\n", backendunits);
diff --git a/gl_backend.h b/gl_backend.h
index 0c66d2f1..6dd8d0ae 100644
--- a/gl_backend.h
+++ b/gl_backend.h
@@ -2,7 +2,8 @@
 #ifndef GL_BACKEND_H
 #define GL_BACKEND_H
 
-#define MAX_TEXTUREUNITS 16
+// how many texture units to track state on (backendunits/backendimageunits/backendarrayunits are limited to this value)
+#define MAX_TEXTUREUNITS 64
 
 #define POLYGONELEMENTS_MAXPOINTS 258
 extern int polygonelements[(POLYGONELEMENTS_MAXPOINTS-2)*3];
-- 
2.39.5