Ver código fonte

changing uniform transfes...

leonsal 8 anos atrás
pai
commit
f981c3d183

+ 6 - 0
gls/gls.go

@@ -589,6 +589,12 @@ func (gs *GLS) Uniform2fv(location int32, count int32, v []float32) {
 	gs.stats.Unisets++
 }
 
+func (gs *GLS) Uniform2fvUP(location int32, count int32, v unsafe.Pointer) {
+
+	C.glUniform2fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(v))
+	gs.stats.Unisets++
+}
+
 func (gs *GLS) Uniform3fv(location int32, count int32, v []float32) {
 
 	C.glUniform3fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(&v[0]))

+ 9 - 8
renderer/shaders/include/material.glsl

@@ -1,10 +1,11 @@
 //
 // Material properties uniform
 //
+
+// Material parameters uniform array
 uniform vec3 Material[6];
-// Macros to access elements inside the MatTexinfo array
-// Each texture uses 3 vec2 elements.
-#define MatAmbientColor     Material[0]
+// Macros to access elements inside the Material array
+#define MatAmbientColor		Material[0]
 #define MatDiffuseColor     Material[1]
 #define MatSpecularColor    Material[2]
 #define MatEmissiveColor    Material[3]
@@ -17,12 +18,12 @@ uniform vec3 Material[6];
     // Texture unit sampler array
     uniform sampler2D MatTexture[MAT_TEXTURES];
     // Texture parameters (3*vec2 per texture)
-    uniform mat3 MatTexinfo[MAT_TEXTURES];
+    uniform vec2 MatTexinfo[3*MAT_TEXTURES];
     // Macros to access elements inside the MatTexinfo array
-    #define MatTexOffset(a)     MatTexinfo[a][0].xy
-    #define MatTexRepeat(a)     MatTexinfo[a][1].xy
-    #define MatTexFlipY(a)      bool(MatTexinfo[a][2].x)
-    #define MatTexVisible(a)    bool(MatTexinfo[a][2].y)
+    #define MatTexOffset(a)		MatTexinfo[(3*a)]
+    #define MatTexRepeat(a)		MatTexinfo[(3*a)+1]
+    #define MatTexFlipY(a)		bool(MatTexinfo[(3*a)+2].x)
+    #define MatTexVisible(a)	bool(MatTexinfo[(3*a)+2].y)
 #endif
 
 // GLSL 3.30 does not allow indexing texture sampler with non constant values.

+ 10 - 8
renderer/shaders/sources.go

@@ -124,10 +124,11 @@ void phongModel(vec4 position, vec3 normal, vec3 camDir, vec3 matAmbient, vec3 m
 const include_material_source = `//
 // Material properties uniform
 //
+
+// Material parameters uniform array
 uniform vec3 Material[6];
-// Macros to access elements inside the MatTexinfo array
-// Each texture uses 3 vec2 elements.
-#define MatAmbientColor     Material[0]
+// Macros to access elements inside the Material array
+#define MatAmbientColor		Material[0]
 #define MatDiffuseColor     Material[1]
 #define MatSpecularColor    Material[2]
 #define MatEmissiveColor    Material[3]
@@ -140,12 +141,13 @@ uniform vec3 Material[6];
     // Texture unit sampler array
     uniform sampler2D MatTexture[MAT_TEXTURES];
     // Texture parameters (3*vec2 per texture)
-    uniform mat3 MatTexinfo[MAT_TEXTURES];
+    uniform vec2 MatTexinfo[3*MAT_TEXTURES];
     // Macros to access elements inside the MatTexinfo array
-    #define MatTexOffset(a)     MatTexinfo[a][0].xy
-    #define MatTexRepeat(a)     MatTexinfo[a][1].xy
-    #define MatTexFlipY(a)      bool(MatTexinfo[a][2].x)
-    #define MatTexVisible(a)    bool(MatTexinfo[a][2].y)
+    // Each texture uses 3 vec2 elements.
+    #define MatTexOffset(a)		MatTexinfo[(3*a)]
+    #define MatTexRepeat(a)		MatTexinfo[(3*a)+1]
+    #define MatTexFlipY(a)		bool(MatTexinfo[(3*a)+2].x)
+    #define MatTexVisible(a)	bool(MatTexinfo[(3*a)+2].y)
 #endif
 
 // GLSL 3.30 does not allow indexing texture sampler with non constant values.

+ 55 - 52
texture/texture2D.go

@@ -6,45 +6,46 @@ package texture
 
 import (
 	"fmt"
-	"github.com/g3n/engine/gls"
 	"image"
 	"image/draw"
 	_ "image/gif"
 	_ "image/jpeg"
 	_ "image/png"
 	"os"
+	"unsafe"
+
+	"github.com/g3n/engine/gls"
 )
 
 type Texture2D struct {
-	gs           *gls.GLS            // Pointer to OpenGL state
-	refcount     int                 // Current number of references
-	texname      uint32              // Texture handle
-	magFilter    uint32              // magnification filter
-	minFilter    uint32              // minification filter
-	wrapS        uint32              // wrap mode for s coordinate
-	wrapT        uint32              // wrap mode for t coordinate
-	iformat      int32               // internal format
-	width        int32               // texture width in pixels
-	height       int32               // texture height in pixels
-	format       uint32              // format of the pixel data
-	formatType   uint32              // type of the pixel data
-	updateData   bool                // texture data needs to be sent
-	updateParams bool                // texture parameters needs to be sent
-	genMipmap    bool                // generate mipmaps flag
-	data         interface{}         // array with texture data
-	uTexture     gls.Uniform1i       // Texture unit uniform
-	uTexinfo     gls.UniformMatrix3f // uniform 3x3 array with texture info
+	gs           *gls.GLS     // Pointer to OpenGL state
+	refcount     int          // Current number of references
+	texname      uint32       // Texture handle
+	magFilter    uint32       // magnification filter
+	minFilter    uint32       // minification filter
+	wrapS        uint32       // wrap mode for s coordinate
+	wrapT        uint32       // wrap mode for t coordinate
+	iformat      int32        // internal format
+	width        int32        // texture width in pixels
+	height       int32        // texture height in pixels
+	format       uint32       // format of the pixel data
+	formatType   uint32       // type of the pixel data
+	updateData   bool         // texture data needs to be sent
+	updateParams bool         // texture parameters needs to be sent
+	genMipmap    bool         // generate mipmaps flag
+	data         interface{}  // array with texture data
+	uniUnit      gls.Uniform2 // Texture unit uniform location cache
+	uniInfo      gls.Uniform2 // Texture info uniform location cache
+	udata        struct {     // Combined uniform data in 3 vec2:
+		offsetX float32
+		offsetY float32
+		repeatX float32
+		repeatY float32
+		flipY   float32
+		visible float32
+	}
 }
 
-const (
-	iOffsetX = 0
-	iOffsetY = 1
-	iRepeatX = 3
-	iRepeatY = 4
-	iFlipY   = 6
-	iVisible = 7
-)
-
 func newTexture2D() *Texture2D {
 
 	t := new(Texture2D)
@@ -60,15 +61,12 @@ func newTexture2D() *Texture2D {
 	t.genMipmap = true
 
 	// Initialize Uniform elements
-	t.uTexture.Init("MatTexture")
-	t.uTexinfo.Init("MatTexinfo")
-	t.uTexinfo.Set(iOffsetX, 0)
-	t.uTexinfo.Set(iOffsetY, 0)
-	t.uTexinfo.Set(iRepeatX, 1)
-	t.uTexinfo.Set(iRepeatY, 1)
-	t.uTexinfo.Set(iFlipY, 1)
-	t.uTexinfo.Set(iVisible, 1)
-
+	t.uniUnit.Init("MatTexture")
+	t.uniInfo.Init("MatTexinfo")
+	t.SetOffset(0, 0)
+	t.SetRepeat(0, 0)
+	t.SetFlipY(true)
+	t.SetVisible(true)
 	return t
 }
 
@@ -170,16 +168,16 @@ func (t *Texture2D) SetData(width, height int, format int, formatType, iformat i
 func (t *Texture2D) SetVisible(state bool) {
 
 	if state {
-		t.uTexinfo.Set(iVisible, 1)
+		t.udata.visible = 1
 	} else {
-		t.uTexinfo.Set(iVisible, 0)
+		t.udata.visible = 0
 	}
 }
 
 // Visible returns the current visibility state of the texture
 func (t *Texture2D) Visible() bool {
 
-	if t.uTexinfo.Get(iVisible) == 0 {
+	if t.udata.visible == 0 {
 		return false
 	} else {
 		return true
@@ -221,36 +219,36 @@ func (t *Texture2D) SetWrapT(wrapT uint32) {
 // SetRepeat set the repeat factor
 func (t *Texture2D) SetRepeat(x, y float32) {
 
-	t.uTexinfo.Set(iRepeatX, x)
-	t.uTexinfo.Set(iRepeatY, y)
+	t.udata.repeatX = x
+	t.udata.repeatY = y
 }
 
 // Repeat returns the current X and Y repeat factors
 func (t *Texture2D) Repeat() (float32, float32) {
 
-	return t.uTexinfo.Get(iRepeatX), t.uTexinfo.Get(iRepeatY)
+	return t.udata.repeatX, t.udata.repeatY
 }
 
 // SetOffset sets the offset factor
 func (t *Texture2D) SetOffset(x, y float32) {
 
-	t.uTexinfo.Set(iOffsetX, x)
-	t.uTexinfo.Set(iOffsetY, y)
+	t.udata.offsetX = x
+	t.udata.offsetY = y
 }
 
 // Offset returns the current X and Y offset factors
 func (t *Texture2D) Offset() (float32, float32) {
 
-	return t.uTexinfo.Get(iOffsetX), t.uTexinfo.Get(iOffsetY)
+	return t.udata.offsetX, t.udata.offsetY
 }
 
 // SetFlipY set the state for flipping the Y coordinate
 func (t *Texture2D) SetFlipY(state bool) {
 
 	if state {
-		t.uTexinfo.Set(iFlipY, 1)
+		t.udata.flipY = 1
 	} else {
-		t.uTexinfo.Set(iFlipY, 0)
+		t.udata.flipY = 0
 	}
 }
 
@@ -338,8 +336,13 @@ func (t *Texture2D) RenderSetup(gs *gls.GLS, idx int) {
 		t.updateParams = false
 	}
 
-	// Transfer uniforms
-	t.uTexture.Set(int32(idx))
-	t.uTexture.TransferIdx(gs, idx)
-	t.uTexinfo.TransferIdx(gs, idx)
+	// Transfer texture unit uniform
+	location := t.uniUnit.LocationIdx(gs, int32(idx))
+	gs.Uniform1i(location, int32(idx))
+
+	// Transfer texture info combined uniform
+	const vec2count = 3
+	location = t.uniInfo.LocationIdx(gs, vec2count*int32(idx))
+	log.Error("location:%v count:%v udata:%v", location, vec2count, t.udata)
+	gs.Uniform2fvUP(location, vec2count, unsafe.Pointer(&t.udata))
 }