Browse Source

Updated js texture creation and converted texture upload calls to texSubImage2D/3D to handle immutable texture upload

Clement Espeute 2 years ago
parent
commit
4f46f35828
1 changed files with 19 additions and 23 deletions
  1. 19 23
      h3d/impl/GlDriver.hx

+ 19 - 23
h3d/impl/GlDriver.hx

@@ -953,6 +953,21 @@ class GlDriver extends Driver {
 		gl.texParameteri(bind, GL.TEXTURE_BASE_LEVEL, 0);
 		gl.texParameteri(bind, GL.TEXTURE_BASE_LEVEL, 0);
 		gl.texParameteri(bind, GL.TEXTURE_MAX_LEVEL, t.mipLevels-1);
 		gl.texParameteri(bind, GL.TEXTURE_MAX_LEVEL, t.mipLevels-1);
 		#end
 		#end
+
+		#if js
+		// Modern texture allocation that supports both compressed and uncompressed texture in WebGL
+		// texStorate2D/3D is only defined in OpenGL 4.2 but is defined in openGL ES 3 which the js target targets
+
+		// Patch RGBA to be RGBA8 because texStorage expect a "Sized Internal Format"
+		var sizedFormat = tt.internalFmt == GL.RGBA ? GL.RGBA8 : tt.internalFmt;
+		if( t.flags.has(IsArray) && !t.flags.has(Cube) ) {
+			gl.texStorage3D(bind, t.mipLevels, sizedFormat, tt.width, tt.height, t.layerCount);
+			checkError();
+		} else {
+			gl.texStorage2D(bind, t.mipLevels, sizedFormat, tt.width, tt.height);
+			checkError();
+		}
+		#else
 		for(mip in 0...t.mipLevels) {
 		for(mip in 0...t.mipLevels) {
 			var w = hxd.Math.imax(1, tt.width >> mip);
 			var w = hxd.Math.imax(1, tt.width >> mip);
 			var h = hxd.Math.imax(1, tt.height >> mip);
 			var h = hxd.Math.imax(1, tt.height >> mip);
@@ -962,27 +977,13 @@ class GlDriver extends Driver {
 					if( checkError() ) break;
 					if( checkError() ) break;
 				}
 				}
 			} else if( t.flags.has(IsArray) ) {
 			} else if( t.flags.has(IsArray) ) {
-				#if js
-				if( !t.format.match(S3TC(_)) )
-				#end
 				gl.texImage3D(bind, mip, tt.internalFmt, w, h, t.layerCount, 0, getChannels(tt), tt.pixelFmt, null);
 				gl.texImage3D(bind, mip, tt.internalFmt, w, h, t.layerCount, 0, getChannels(tt), tt.pixelFmt, null);
 				checkError();
 				checkError();
 			} else {
 			} else {
-				#if js
-				if( !t.format.match(S3TC(_)) )
-				#end
 				gl.texImage2D(bind, mip, tt.internalFmt, w, h, 0, getChannels(tt), tt.pixelFmt, null);
 				gl.texImage2D(bind, mip, tt.internalFmt, w, h, 0, getChannels(tt), tt.pixelFmt, null);
 				checkError();
 				checkError();
 			}
 			}
 		}
 		}
-
-		#if js
-		if( t.flags.has(IsArray) ) {
-			if (t.format.match(S3TC(_))) {
-				gl.texStorage3D(bind, t.mipLevels, tt.internalFmt, tt.width, tt.height, t.layerCount);
-				checkError();
-			}
-		}
 		#end
 		#end
 
 
 		restoreBind();
 		restoreBind();
@@ -1143,7 +1144,7 @@ class GlDriver extends Driver {
 		} else {
 		} else {
 			var img = bmp.toNative();
 			var img = bmp.toNative();
 			gl.bindTexture(GL.TEXTURE_2D, t.t.t);
 			gl.bindTexture(GL.TEXTURE_2D, t.t.t);
-			gl.texImage2D(GL.TEXTURE_2D, mipLevel, t.t.internalFmt, getChannels(t.t), t.t.pixelFmt, img.getImageData(0, 0, bmp.width, bmp.height));
+			gl.texSubImage2D(GL.TEXTURE_2D, mipLevel, 0, 0, getChannels(t.t), t.t.pixelFmt, img.getImageData(0, 0, bmp.width, bmp.height));
 			restoreBind();
 			restoreBind();
 		}
 		}
 	#end
 	#end
@@ -1250,12 +1251,12 @@ class GlDriver extends Driver {
 			if( t.flags.has(IsArray) )
 			if( t.flags.has(IsArray) )
 				gl.compressedTexSubImage3D(face, mipLevel, 0, 0, side, pixels.width, pixels.height, 1, t.t.internalFmt, buffer);
 				gl.compressedTexSubImage3D(face, mipLevel, 0, 0, side, pixels.width, pixels.height, 1, t.t.internalFmt, buffer);
 			else
 			else
-				gl.compressedTexImage2D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, 0, buffer);
+				gl.compressedTexSubImage2D(face, mipLevel, 0, 0, pixels.width, pixels.height, t.t.internalFmt, buffer);
 		} else {
 		} else {
 			if( t.flags.has(IsArray) )
 			if( t.flags.has(IsArray) )
 				gl.texSubImage3D(face, mipLevel, 0, 0, side, pixels.width, pixels.height, 1, getChannels(t.t), t.t.pixelFmt, buffer);
 				gl.texSubImage3D(face, mipLevel, 0, 0, side, pixels.width, pixels.height, 1, getChannels(t.t), t.t.pixelFmt, buffer);
 			else
 			else
-				gl.texImage2D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, 0, getChannels(t.t), t.t.pixelFmt, buffer);
+				gl.texSubImage2D(face, mipLevel, 0, 0, pixels.width, pixels.height, getChannels(t.t), t.t.pixelFmt, buffer);
 		}
 		}
 		#else
 		#else
 		throw "Not implemented";
 		throw "Not implemented";
@@ -1769,12 +1770,7 @@ class GlDriver extends Driver {
 		if( t.flags.has(IsArray) ) throw "TODO:texImage3D";
 		if( t.flags.has(IsArray) ) throw "TODO:texImage3D";
 		var face = cubic ? CUBE_FACES[side] : GL.TEXTURE_2D;
 		var face = cubic ? CUBE_FACES[side] : GL.TEXTURE_2D;
 		gl.bindTexture(bind, t.t.t);
 		gl.bindTexture(bind, t.t.t);
-		if (glES >= 3) {
-			// WebGL2 support
-			gl.texImage2D(face, mipLevel, t.t.internalFmt, v.videoWidth, v.videoHeight, 0, getChannels(t.t), t.t.pixelFmt, untyped v);
-		} else {
-			gl.texImage2D(face, mipLevel, t.t.internalFmt, t.t.internalFmt, t.t.pixelFmt, v);
-		}
+		gl.texSubImage2D(face, mipLevel, 0, 0, v.videoWidth, v.videoHeight, getChannels(t.t), t.t.pixelFmt, untyped v);
 		restoreBind();
 		restoreBind();
 	}
 	}