Skip to content

Commit 8667ce0

Browse files
robertleeplummerjrdhritzkiv
authored andcommitted
fix: #178 add in cache for stencils so calls aren't as excessive
1 parent 53766a1 commit 8667ce0

File tree

4 files changed

+128
-2
lines changed

4 files changed

+128
-2
lines changed

src/javascript/node-index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ function createContext (width, height, options) {
7272
ctx._activeArrayBuffer = null
7373
ctx._activeElementArrayBuffer = null
7474
ctx._activeRenderbuffer = null
75+
ctx._checkStencil = false
76+
ctx._stencilState = true
7577

7678
// Initialize texture units
7779
const numTextures = ctx.getParameter(ctx.MAX_COMBINED_TEXTURE_IMAGE_UNITS)

src/javascript/webgl-rendering-context.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,21 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
194194
}
195195

196196
_checkStencilState () {
197+
if (!this._checkStencil) {
198+
return this._stencilState
199+
}
200+
this._checkStencil = false
201+
this._stencilState = true
197202
if (this.getParameter(gl.STENCIL_WRITEMASK) !==
198203
this.getParameter(gl.STENCIL_BACK_WRITEMASK) ||
199204
this.getParameter(gl.STENCIL_VALUE_MASK) !==
200205
this.getParameter(gl.STENCIL_BACK_VALUE_MASK) ||
201206
this.getParameter(gl.STENCIL_REF) !==
202207
this.getParameter(gl.STENCIL_BACK_REF)) {
203208
this.setError(gl.INVALID_OPERATION)
204-
return false
209+
this._stencilState = false
205210
}
206-
return true
211+
return this._stencilState
207212
}
208213

209214
_checkTextureTarget (target) {
@@ -1396,6 +1401,7 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
13961401
}
13971402

13981403
clearStencil (s) {
1404+
this._checkStencil = false
13991405
return super.clearStencil(s | 0)
14001406
}
14011407

@@ -3034,26 +3040,32 @@ class WebGLRenderingContext extends NativeWebGLRenderingContext {
30343040
}
30353041

30363042
stencilFunc (func, ref, mask) {
3043+
this._checkStencil = true
30373044
return super.stencilFunc(func | 0, ref | 0, mask | 0)
30383045
}
30393046

30403047
stencilFuncSeparate (face, func, ref, mask) {
3048+
this._checkStencil = true
30413049
return super.stencilFuncSeparate(face | 0, func | 0, ref | 0, mask | 0)
30423050
}
30433051

30443052
stencilMask (mask) {
3053+
this._checkStencil = true
30453054
return super.stencilMask(mask | 0)
30463055
}
30473056

30483057
stencilMaskSeparate (face, mask) {
3058+
this._checkStencil = true
30493059
return super.stencilMaskSeparate(face | 0, mask | 0)
30503060
}
30513061

30523062
stencilOp (fail, zfail, zpass) {
3063+
this._checkStencil = true
30533064
return super.stencilOp(fail | 0, zfail | 0, zpass | 0)
30543065
}
30553066

30563067
stencilOpSeparate (face, fail, zfail, zpass) {
3068+
this._checkStencil = true
30573069
return super.stencilOpSeparate(face | 0, fail | 0, zfail | 0, zpass | 0)
30583070
}
30593071

test/misc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ var BLACKLIST = [
77
require('./util/conformance')(function (str) {
88
return str.indexOf('misc') === 0 && BLACKLIST.indexOf(str) < 0
99
})
10+
11+
require('./util/stencil-check-cache')

test/util/stencil-check-cache.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
var tape = require('tape')
2+
var { WebGLRenderingContext } = require('../../src/javascript/webgl-rendering-context')
3+
var createContext = require('../../src/javascript/node-index')
4+
5+
tape('stencil check cache - gl.stencilFunc()', function (t) {
6+
var gl = new WebGLRenderingContext()
7+
t.equals(gl._checkStencil, undefined, 'gl._checkStencil starts undefined')
8+
gl.destroy()
9+
t.end()
10+
})
11+
12+
tape('stencil check cache - gl.stencilFunc()', function (t) {
13+
var gl = new WebGLRenderingContext()
14+
gl.stencilFunc()
15+
t.equals(gl._checkStencil, true, 'gl.stencilFunc() calls set gl._checkStencil to true')
16+
gl.destroy()
17+
t.end()
18+
})
19+
20+
tape('stencil check cache - gl.stencilFuncSeparate()', function (t) {
21+
var gl = new WebGLRenderingContext()
22+
gl.stencilFuncSeparate()
23+
t.equals(gl._checkStencil, true, 'gl.stencilFuncSeparate() calls set gl._checkStencil to true')
24+
gl.destroy()
25+
t.end()
26+
})
27+
28+
tape('stencil check cache - gl.stencilMask()', function (t) {
29+
var gl = new WebGLRenderingContext()
30+
gl.stencilMask()
31+
t.equals(gl._checkStencil, true, 'gl.stencilMask() calls set gl._checkStencil to true')
32+
gl.destroy()
33+
t.end()
34+
})
35+
36+
tape('stencil check cache - gl.stencilMaskSeparate()', function (t) {
37+
var gl = new WebGLRenderingContext()
38+
gl.stencilMaskSeparate()
39+
t.equals(gl._checkStencil, true, 'gl.stencilMaskSeparate() calls set gl._checkStencil to true')
40+
gl.destroy()
41+
t.end()
42+
})
43+
44+
tape('stencil check cache - gl.stencilOp()', function (t) {
45+
var gl = new WebGLRenderingContext()
46+
gl.stencilOp()
47+
t.equals(gl._checkStencil, true, 'gl.stencilOp() calls set gl._checkStencil to true')
48+
gl.destroy()
49+
t.end()
50+
})
51+
52+
tape('stencil check cache - gl.stencilOpSeparate()', function (t) {
53+
var gl = new WebGLRenderingContext()
54+
gl.stencilOpSeparate()
55+
t.equals(gl._checkStencil, true, 'gl.stencilOpSeparate() calls set gl._checkStencil to true')
56+
gl.destroy()
57+
t.end()
58+
})
59+
60+
tape('stencil check cache - gl.clearStencil()', function (t) {
61+
var gl = new WebGLRenderingContext()
62+
gl.clearStencil()
63+
t.equals(gl._checkStencil, false, 'gl.clearStencil() calls set gl._checkStencil to false')
64+
gl.destroy()
65+
t.end()
66+
})
67+
68+
tape('stencil check cache - gl._checkStencilState() without errors', function (t) {
69+
var gl = new WebGLRenderingContext()
70+
gl._checkStencil = true
71+
t.equals(gl._checkStencilState(), true, 'gl._checkStencilState() value is cached and returned')
72+
t.equals(gl._checkStencil, false, 'gl._checkStencilState() calls set gl._checkStencil to false')
73+
t.equals(gl._stencilState, true, 'gl._checkStencilState() calls set gl._stencilState to true')
74+
gl._stencilState = 'test value'
75+
gl.getParameter = function () {
76+
throw new Error('should not be called!')
77+
}
78+
t.equals(gl._checkStencilState(), 'test value', 'subsequent gl._checkStencilState() calls use the cached state stored in gl._stencilState')
79+
gl.destroy()
80+
t.end()
81+
})
82+
83+
tape('stencil check cache - gl._checkStencilState() with errors', function (t) {
84+
var gl = new WebGLRenderingContext()
85+
gl._checkStencil = true
86+
gl.getParameter = function (stencil) {
87+
if (stencil === gl.STENCIL_WRITEMASK) return 1
88+
}
89+
t.equals(gl._checkStencilState(), false, 'gl._checkStencilState() value is cached and returned')
90+
t.equals(gl._checkStencil, false, 'gl._checkStencilState() calls set gl._checkStencil to false')
91+
t.equals(gl._stencilState, false, 'gl._checkStencilState() calls set gl._stencilState to true')
92+
gl._stencilState = 'test value'
93+
gl.getParameter = function () {
94+
throw new Error('should not be called!')
95+
}
96+
t.equals(gl._checkStencilState(), 'test value', 'subsequent gl._checkStencilState() calls use the cached state stored in gl._stencilState')
97+
gl.destroy()
98+
t.end()
99+
})
100+
101+
tape('stencil check cache - createContext initial state', function (t) {
102+
var gl = createContext(1, 1)
103+
gl.getParameter = function () {
104+
throw new Error('should not be called!')
105+
}
106+
gl._stencilState = 'test value'
107+
t.equals(gl._checkStencilState(), true, 'initial call to createContext()._checkStencilState() return gl._stencilState and not call gl.getParameter()')
108+
gl.destroy()
109+
t.end()
110+
})

0 commit comments

Comments
 (0)