Browse Source

opengl: fix uploadTexturePixels for texture arrays

trethaller 4 years ago
parent
commit
60ebd027e1
1 changed files with 21 additions and 6 deletions
  1. 21 6
      h3d/impl/GlDriver.hx

+ 21 - 6
h3d/impl/GlDriver.hx

@@ -20,6 +20,8 @@ private extern class GL2 extends js.html.webgl.GL {
 	function framebufferTextureLayer( target : Int, attach : Int, t : js.html.webgl.Texture, level : Int, layer : Int ) : Void;
 	function texImage3D(target : Int, level : Int, internalformat : Int, width : Int, height : Int, depth : Int, border : Int, format : Int, type : Int, source : Dynamic) : Void;
 	function compressedTexImage3D(target : Int, level : Int, internalformat : Int, width : Int, height : Int, depth : Int, border : Int, source : Dynamic) : Void;
+	function texSubImage3D( target : Int, level : Int, xoffset : Int, yoffset : Int, zoffset : Int, width : Int, height : Int, depth : Int, format : Int, type : Int, image : Dynamic ) : Void;
+	function compressedTexSubImage3D( target : Int, level : Int, xoffset : Int, yoffset : Int, zoffset : Int, width : Int, height : Int, depth : Int, format : Int, image : Dynamic ) : Void;
 	static inline var RGBA16F = 0x881A;
 	static inline var RGBA32F = 0x8814;
 	static inline var RED      = 0x1903;
@@ -1165,8 +1167,8 @@ class GlDriver extends Driver {
 
 	override function uploadTexturePixels( t : h3d.mat.Texture, pixels : hxd.Pixels, mipLevel : Int, side : Int ) {
 		var cubic = t.flags.has(Cube);
+		var face = cubic ? CUBE_FACES[side] : t.flags.has(IsArray) ? GL2.TEXTURE_2D_ARRAY : GL.TEXTURE_2D;
 		var bind = getBindType(t);
-		var face = cubic ? CUBE_FACES[side] : GL.TEXTURE_2D;
 		gl.bindTexture(bind, t.t.t);
 		pixels.convert(t.format);
 		pixels.setFlip(false);
@@ -1178,13 +1180,13 @@ class GlDriver extends Driver {
 			throw "Compressed textures require hlsdl 1.10+";
 			#else
 			if( t.flags.has(IsArray) )
-				gl.compressedTexImage3D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, side, 0, dataLen, stream);
+				gl.compressedTexSubImage3D(face, mipLevel, 0, 0, side, pixels.width, pixels.height, 1, t.t.internalFmt, dataLen, stream);
 			else
 				gl.compressedTexImage2D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, 0, dataLen, stream);
 			#end
 		} else {
 			if( t.flags.has(IsArray) )
-				gl.texImage3D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, side, 0, getChannels(t.t), t.t.pixelFmt, stream);
+				gl.texSubImage3D(face, mipLevel, 0, 0, side, pixels.width, pixels.height, 1, getChannels(t.t), t.t.pixelFmt, stream);
 			else
 				gl.texImage2D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, 0, getChannels(t.t), t.t.pixelFmt, stream);
 		}
@@ -1197,7 +1199,6 @@ class GlDriver extends Driver {
 			pixels = pixels.clone();
 		}
 		#end
-		if( t.flags.has(IsArray) ) throw "TODO:texImage3D";
 		var buffer : ArrayBufferView = switch( t.format ) {
 		case RGBA32F, R32F, RG32F, RGB32F: new Float32Array(@:privateAccess pixels.bytes.b.buffer, pixels.offset, dataLen>>2);
 		case RGBA16F, R16F, RG16F, RGB16F: new Uint16Array(@:privateAccess pixels.bytes.b.buffer, pixels.offset, dataLen>>1);
@@ -1206,12 +1207,12 @@ class GlDriver extends Driver {
 		}
 		if( t.format.match(S3TC(_)) ) {
 			if( t.flags.has(IsArray) )
-				gl.compressedTexImage3D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, side, 0, buffer);
+				gl.compressedTexSubImage3D(face, mipLevel, 0, 0, side, pixels.width, pixels.height, 1, t.t.internalFmt, buffer);
 			else
 				gl.compressedTexImage2D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, 0, buffer);
 		} else {
 			if( t.flags.has(IsArray) )
-				gl.texImage3D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, side, 0, 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
 				gl.texImage2D(face, mipLevel, t.t.internalFmt, pixels.width, pixels.height, 0, getChannels(t.t), t.t.pixelFmt, buffer);
 		}
@@ -1761,6 +1762,20 @@ class GlDriver extends Driver {
 		return GL.queryResult(q.q);
 	}
 
+	inline function debugCheckError() {
+		if(!debug) return;
+		var err = gl.getError();
+		if(err != GL.NO_ERROR) {
+			switch(err) {
+				case GL.INVALID_ENUM: throw "INVALID_ENUM";
+				case GL.INVALID_VALUE: throw "INVALID_VALUE";
+				case GL.INVALID_OPERATION: throw "INVALID_OPERATION";
+				case 1286: throw "INVALID_FRAMEBUFFER_OPERATION";
+				default: throw "Error: " + err;
+			}
+		}
+	}
+
 	#end
 
 	static var TFILTERS = [