소스 검색

methods for internal checkErrors flag

leonsal 8 년 전
부모
커밋
1e833efe97
2개의 변경된 파일80개의 추가작업 그리고 92개의 파일을 삭제
  1. 59 71
      gls/gls.go
  2. 21 21
      gls/program.go

+ 59 - 71
gls/gls.go

@@ -2,45 +2,37 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Encapsulates the raw OpenGL in a more friendly API and
-// keeps some state to minimize function calling
-//
+// Package gls allows access to the OpenGL functions.
 package gls
 
 import (
 	"github.com/g3n/engine/util/logger"
 	"github.com/go-gl/gl/v3.3-core/gl"
+	"math"
 )
 
+// GLS allows access to the OpenGL functions and keeps state to
+// minimize functions calling.
+// It also keeps some statistics of some OpenGL objects currently allocated
 type GLS struct {
-	// Check openGL API errors flags
-	CheckErrors bool
-	// Current active program
-	Prog *Program
-	// Programs cache
-	Programs map[*Program]bool
 	// Statistics
 	Stats struct {
-		Vaos     int
-		Vbos     int
-		Textures int
+		Vaos     int // Number of Vertex Array Objects
+		Vbos     int // Number of Vertex Buffer Objects
+		Textures int // Number of Textures
 	}
-	// Current view port
-	viewportX      int32
-	viewportY      int32
-	viewportWidth  int32
-	viewportHeight int32
-	// Current clear color
-	clearColorR  float32
-	clearColorG  float32
-	clearColorB  float32
-	clearColorA  float32
-	lineWidth    float32
-	sideView     int
-	depthFunc    uint32
-	depthMask    int
-	capabilities map[int]int
-	// Current blending state
+	Prog               *Program          // Current active program
+	programs           map[*Program]bool // Programs cache
+	checkErrors        bool              // Check openGL API errors flag
+	viewportX          int32
+	viewportY          int32
+	viewportWidth      int32
+	viewportHeight     int32
+	lineWidth          float32
+	sideView           int
+	depthFunc          uint32
+	depthMask          int
+	capabilities       map[int]int
 	blendEquation      uint32
 	blendSrc           uint32
 	blendDst           uint32
@@ -52,16 +44,15 @@ type GLS struct {
 	blendDstAlpha      uint32
 }
 
-// Internal capability enable/disabled state
 const (
 	capUndef    = 0
 	capDisabled = 1
 	capEnabled  = 2
 )
 const (
-	intUndef = -1
-	intFalse = 0
-	intTrue  = 1
+	uintUndef = math.MaxUint32
+	intFalse  = 0
+	intTrue   = 1
 )
 
 // Polygon side view.
@@ -72,12 +63,12 @@ const (
 )
 
 // Package logger
-var log = logger.New("GL", logger.Default)
+var log = logger.New("GLS", logger.Default)
 
-//
 // New creates and returns a new instance of an GLS object
 // which encapsulates the state of an OpenGL context
-//
+// This should be called only after an active OpenGL context
+// was established, such as by creating a new window.
 func New() (*GLS, error) {
 
 	gs := new(GLS)
@@ -88,31 +79,45 @@ func New() (*GLS, error) {
 	if err != nil {
 		return nil, err
 	}
-	gs.CheckErrors = true
+	gs.SetDefaultState()
+	gs.checkErrors = true
 	return gs, nil
 }
 
-//
+// SetCheckErrors enables/disables checking for errors after the
+// call of any OpenGL function. It is enabled by default but
+// could be disabled after an application is stable to improve the performance.
+func (gs *GLS) SetCheckErrors(enable bool) {
+
+	gs.checkErrors = enable
+}
+
+// ChecksErrors returns if error checking is enabled or not.
+func (gs *GLS) CheckErrors() bool {
+
+	return gs.checkErrors
+}
+
 // Reset resets the internal state kept of the OpenGL
-//
 func (gs *GLS) Reset() {
 
 	gs.lineWidth = 0.0
-	gs.sideView = intUndef
+	gs.sideView = uintUndef
 	gs.depthFunc = 0
-	gs.depthMask = intUndef
+	gs.depthMask = uintUndef
 	gs.capabilities = make(map[int]int)
-	gs.Programs = make(map[*Program]bool)
+	gs.programs = make(map[*Program]bool)
+	gs.Prog = nil
 
-	//gs.blendEquation = intUndef
-	//gs.blendSrc = intUndef
-	//gs.blendDst = intUndef
-	//gs.blendEquationRGB = 0
-	//gs.blendEquationAlpha = 0
-	//gs.blendSrcRGB = intUndef
-	//gs.blendSrcAlpha = intUndef
-	//gs.blendDstRGB = intUndef
-	//gs.blendDstAlpha = intUndef
+	gs.blendEquation = uintUndef
+	gs.blendSrc = uintUndef
+	gs.blendDst = uintUndef
+	gs.blendEquationRGB = 0
+	gs.blendEquationAlpha = 0
+	gs.blendSrcRGB = uintUndef
+	gs.blendSrcAlpha = uintUndef
+	gs.blendDstRGB = uintUndef
+	gs.blendDstAlpha = uintUndef
 }
 
 func (gs *GLS) SetDefaultState() {
@@ -120,7 +125,6 @@ func (gs *GLS) SetDefaultState() {
 	gl.ClearColor(0, 0, 0, 1)
 	gl.ClearDepth(1)
 	gl.ClearStencil(0)
-
 	gs.Enable(gl.DEPTH_TEST)
 	gs.DepthFunc(gl.LEQUAL)
 	gl.FrontFace(gl.CCW)
@@ -132,9 +136,6 @@ func (gs *GLS) SetDefaultState() {
 	gs.Enable(gl.VERTEX_PROGRAM_POINT_SIZE)
 	gs.Enable(gl.PROGRAM_POINT_SIZE)
 	gs.Enable(gl.MULTISAMPLE)
-
-	//gl.Viewport(gs.viewportX, gs.viewportY, gs.viewportWidth, gs.viewportHeight)
-	//gl.ClearColor(g.ClearColor.R, g.ClearColor.G, g.ClearColor.B, 1.0)
 }
 
 func (gs *GLS) ActiveTexture(texture uint32) {
@@ -149,21 +150,18 @@ func (gs *GLS) BindBuffer(target int, vbo uint32) {
 	gs.checkError("BindBuffer")
 }
 
-// BindTexture
 func (gs *GLS) BindTexture(target int, tex uint32) {
 
 	gl.BindTexture(uint32(target), tex)
 	gs.checkError("BindTexture")
 }
 
-// Bind Vertex Array Object
 func (gs *GLS) BindVertexArray(vao uint32) {
 
 	gl.BindVertexArray(vao)
 	gs.checkError("BindVertexArray")
 }
 
-// BlendEquation set the equation for blending pixels.
 func (gs *GLS) BlendEquation(mode uint32) {
 
 	if gs.blendEquation == mode {
@@ -218,14 +216,7 @@ func (gs *GLS) BufferData(target uint32, size int, data interface{}, usage uint3
 
 func (gs *GLS) ClearColor(r, g, b, a float32) {
 
-	if gs.clearColorR == a && gs.clearColorG == g && gs.clearColorB == b && gs.clearColorA == a {
-		return
-	}
 	gl.ClearColor(r, g, b, a)
-	gs.clearColorR = r
-	gs.clearColorG = g
-	gs.clearColorB = b
-	gs.clearColorA = a
 }
 
 func (gs *GLS) Clear(mask int) {
@@ -323,7 +314,6 @@ func (gs *GLS) FrontFace(mode uint32) {
 	gs.checkError("FrontFace")
 }
 
-// GenBuffer generates and returns one Vertex Buffer Object name
 func (gs *GLS) GenBuffer() uint32 {
 
 	var buf uint32
@@ -333,14 +323,12 @@ func (gs *GLS) GenBuffer() uint32 {
 	return buf
 }
 
-// GenerateMipmap generates mipmaps for the specified texture object.
 func (gs *GLS) GenerateMipmap(target uint32) {
 
 	gl.GenerateMipmap(target)
 	gs.checkError("GenerateMipmap")
 }
 
-// GenTexture generates and returns one Texture Object name
 func (gs *GLS) GenTexture() uint32 {
 
 	var tex uint32
@@ -495,9 +483,9 @@ func (gs *GLS) UseProgram(prog *Program) {
 	gs.Prog = prog
 
 	// Inserts program in cache if not already there.
-	if !gs.Programs[prog] {
-		gs.Programs[prog] = true
-		log.Warn("New Program activated. Total: %d", len(gs.Programs))
+	if !gs.programs[prog] {
+		gs.programs[prog] = true
+		log.Debug("New Program activated. Total: %d", len(gs.programs))
 	}
 }
 
@@ -520,7 +508,7 @@ func (gs *GLS) Viewport(x, y, width, height int32) {
 // checkError checks the error code of the previously called OpenGL function
 func (gls *GLS) checkError(fname string) {
 
-	if gls.CheckErrors {
+	if gls.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("Error:%d calling:%s()", ecode, fname)

+ 21 - 21
gls/program.go

@@ -155,7 +155,7 @@ func (prog *Program) GetActiveUniformBlockSize(ubindex uint32) int32 {
 
 	var uboSize int32
 	gl.GetActiveUniformBlockiv(prog.handle, ubindex, gl.UNIFORM_BLOCK_DATA_SIZE, &uboSize)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("GetUniformBlockSize(%v) error: %d", ubindex, ecode)
@@ -170,7 +170,7 @@ func (prog *Program) GetActiveUniformsiv(indices []uint32, pname uint32) []int32
 
 	data := make([]int32, len(indices))
 	gl.GetActiveUniformsiv(prog.handle, int32(len(indices)), &indices[0], pname, &data[0])
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("GetActiveUniformsiv() error: %d", ecode)
@@ -193,7 +193,7 @@ func (prog *Program) GetAttribLocation(name string) int32 {
 func (prog *Program) GetUniformBlockIndex(name string) uint32 {
 
 	index := gl.GetUniformBlockIndex(prog.handle, gl.Str(name+"\x00"))
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("GetUniformBlockIndex(%s) error", name)
@@ -215,7 +215,7 @@ func (prog *Program) GetUniformIndices(names []string) []uint32 {
 
 	indices := make([]uint32, len(names))
 	gl.GetUniformIndices(prog.handle, int32(len(names)), unames, &indices[0])
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("GetUniformIndices() error: %d", ecode)
@@ -236,7 +236,7 @@ func (prog *Program) GetUniformLocation(name string) int32 {
 	}
 	// Get location from GL
 	loc = gl.GetUniformLocation(prog.handle, gl.Str(name+"\x00"))
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("GetUniformLocation(%s) error: %d", name, ecode)
@@ -255,7 +255,7 @@ func (prog *Program) GetUniformLocation(name string) int32 {
 func (prog *Program) SetUniformInt(loc int32, v int) {
 
 	gl.Uniform1i(loc, int32(v))
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformInt() error: %d", ecode)
@@ -268,7 +268,7 @@ func (prog *Program) SetUniformInt(loc int32, v int) {
 func (prog *Program) SetUniformFloat(loc int32, v float32) {
 
 	gl.Uniform1f(loc, v)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformFloat() error: %d", ecode)
@@ -281,7 +281,7 @@ func (prog *Program) SetUniformFloat(loc int32, v float32) {
 func (prog *Program) SetUniformVector2(loc int32, v *math32.Vector2) {
 
 	gl.Uniform2f(loc, v.X, v.Y)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformVector2() error: %d", ecode)
@@ -294,7 +294,7 @@ func (prog *Program) SetUniformVector2(loc int32, v *math32.Vector2) {
 func (prog *Program) SetUniformVector3(loc int32, v *math32.Vector3) {
 
 	gl.Uniform3f(loc, v.X, v.Y, v.Z)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformVector3() error: %d", ecode)
@@ -307,7 +307,7 @@ func (prog *Program) SetUniformVector3(loc int32, v *math32.Vector3) {
 func (prog *Program) SetUniformVector4(loc int32, v *math32.Vector4) {
 
 	gl.Uniform4f(loc, v.X, v.Y, v.Z, v.W)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformVector4() error: %d", ecode)
@@ -320,7 +320,7 @@ func (prog *Program) SetUniformVector4(loc int32, v *math32.Vector4) {
 func (prog *Program) SetUniformMatrix3(loc int32, m *math32.Matrix3) {
 
 	gl.UniformMatrix3fv(loc, 1, false, &m[0])
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformMatrix3() error: %d", ecode)
@@ -333,7 +333,7 @@ func (prog *Program) SetUniformMatrix3(loc int32, m *math32.Matrix3) {
 func (prog *Program) SetUniformMatrix4(loc int32, m *math32.Matrix4) {
 
 	gl.UniformMatrix4fv(loc, 1, false, &m[0])
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformMatrix4() error: %d", ecode)
@@ -347,7 +347,7 @@ func (prog *Program) SetUniformMatrix4(loc int32, m *math32.Matrix4) {
 func (prog *Program) SetUniformIntByName(name string, v int) {
 
 	gl.Uniform1i(prog.GetUniformLocation(name), int32(v))
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("GetUniformIntByName(%s) error: %d", name, ecode)
@@ -361,7 +361,7 @@ func (prog *Program) SetUniformIntByName(name string, v int) {
 func (prog *Program) SetUniformFloatByName(name string, v float32) {
 
 	gl.Uniform1f(prog.GetUniformLocation(name), v)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformFloatByName(%s) error: %d", name, ecode)
@@ -375,7 +375,7 @@ func (prog *Program) SetUniformFloatByName(name string, v float32) {
 func (prog *Program) SetUniformVector2ByName(name string, v *math32.Vector2) {
 
 	gl.Uniform2f(prog.GetUniformLocation(name), v.X, v.Y)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformVector2ByName(%s) error: %d", name, ecode)
@@ -389,7 +389,7 @@ func (prog *Program) SetUniformVector2ByName(name string, v *math32.Vector2) {
 func (prog *Program) SetUniformVector3ByName(name string, v *math32.Vector3) {
 
 	gl.Uniform3f(prog.GetUniformLocation(name), v.X, v.Y, v.Z)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformVector3ByName(%s) error: %d", name, ecode)
@@ -403,7 +403,7 @@ func (prog *Program) SetUniformVector3ByName(name string, v *math32.Vector3) {
 func (prog *Program) SetUniformVector4ByName(name string, v *math32.Vector4) {
 
 	gl.Uniform4f(prog.GetUniformLocation(name), v.X, v.Y, v.Z, v.W)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformVector4ByName(%s) error: %d", name, ecode)
@@ -417,7 +417,7 @@ func (prog *Program) SetUniformVector4ByName(name string, v *math32.Vector4) {
 func (prog *Program) SetUniformMatrix3ByName(name string, m *math32.Matrix3) {
 
 	gl.UniformMatrix3fv(prog.GetUniformLocation(name), 1, false, &m[0])
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformMatrix3ByName(%s) error: %d", name, ecode)
@@ -431,7 +431,7 @@ func (prog *Program) SetUniformMatrix3ByName(name string, m *math32.Matrix3) {
 func (prog *Program) SetUniformMatrix4ByName(name string, m *math32.Matrix4) {
 
 	gl.UniformMatrix4fv(prog.GetUniformLocation(name), 1, false, &m[0])
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformMatrix4ByName(%s) error: %d", name, ecode)
@@ -445,7 +445,7 @@ func (prog *Program) SetUniformMatrix4ByName(name string, m *math32.Matrix4) {
 func (prog *Program) SetUniformColorByName(name string, c *math32.Color) {
 
 	gl.Uniform3f(prog.GetUniformLocation(name), c.R, c.G, c.B)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformColorByName(%s) error: %d", name, ecode)
@@ -459,7 +459,7 @@ func (prog *Program) SetUniformColorByName(name string, c *math32.Color) {
 func (prog *Program) SetUniformColor4ByName(name string, c *math32.Color4) {
 
 	gl.Uniform4f(prog.GetUniformLocation(name), c.R, c.G, c.B, c.A)
-	if prog.gs.CheckErrors {
+	if prog.gs.CheckErrors() {
 		ecode := gl.GetError()
 		if ecode != 0 {
 			log.Fatal("SetUniformColor4ByName(%s) error: %d", name, ecode)