Skip to content

Commit 471179f

Browse files
committed
WIP glsl es 3.0 re-integration
1 parent eeeff10 commit 471179f

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

cube/gldemo.frag.glsl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
#version 300 es
2+
3+
precision highp float;
4+
precision highp int;
5+
precision highp sampler2DArray;
6+
17
uniform sampler2D sheet;
28

3-
uniform highp vec2 sheetCellSize;
4-
uniform highp vec2 sheetBounds;
9+
uniform uvec2 sheetBounds;
10+
11+
in vec2 sheetAt;
512

6-
varying highp vec2 sheetAt;
13+
out lowp vec4 color;
714

815
void main(void) {
916
mediump vec2 tileAt = gl_PointCoord;
10-
gl_FragColor = texture2D(sheet, (sheetAt + tileAt) / sheetBounds);
17+
color = texture(sheet,
18+
(sheetAt + tileAt)
19+
/ vec2(float(sheetBounds.x), float(sheetBounds.y))
20+
);
1121
}

cube/gldemo.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ export default async function demo({
101101
};
102102

103103
const uni = {
104-
sheetBounds: mustGetUniform('sheetBounds'), // mediump vec2
104+
sheetBounds: mustGetUniform('sheetBounds'), // uvec2
105105
sheet: mustGetUniform('sheet'), // sampler2D
106106
perspective: mustGetUniform('perspective'), // mediump mat4
107107
};
108108

109109
const attr = {
110-
tileID: mustGetAttr('tileID'), // float
111110
loc: mustGetAttr('loc'), // vec2
112111
size: mustGetAttr('size'), // float
112+
tileID: mustGetAttr('tileID'), // uvec2
113113
};
114114

115115
/// notes from https://webglfundamentals.org/webgl/lessons/webgl-and-alpha.html
@@ -156,7 +156,6 @@ export default async function demo({
156156

157157
let tileSize = 0;
158158
let maxTileID = 0;
159-
let sheetBounds = { x: 0, y: 0 };
160159

161160
const perspective = mat4.identity(new Float32Array(16));
162161

@@ -267,7 +266,7 @@ export default async function demo({
267266
gl.vertexAttribPointer(attr.loc, 2, gl.FLOAT, false, 0, 0);
268267

269268
gl.bindBuffer(gl.ARRAY_BUFFER, tileBuffer)
270-
gl.vertexAttribPointer(attr.tileID, 1, gl.UNSIGNED_SHORT, false, 0, 0);
269+
gl.vertexAttribIPointer(attr.tileID, 1, gl.UNSIGNED_SHORT, 0, 0);
271270

272271
/// per frame drawing pass
273272
gl.clear(gl.COLOR_BUFFER_BIT);
@@ -278,9 +277,9 @@ export default async function demo({
278277
}
279278

280279
function updateSheetTex() {
281-
sheetBounds.x = Math.floor($sheet.width / tileSize);
282-
sheetBounds.y = Math.floor($sheet.height / tileSize);
283-
gl.uniform2f(uni.sheetBounds, sheetBounds.x, sheetBounds.y);
280+
gl.uniform2ui(uni.sheetBounds,
281+
Math.floor($sheet.width / tileSize),
282+
Math.floor($sheet.height / tileSize));
284283

285284
gl.activeTexture(sheetTexUnit);
286285
gl.bindTexture(sheetTexTarget, sheetTex);

cube/gldemo.vert.glsl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
uniform highp vec2 sheetCellSize;
2-
uniform highp vec2 sheetBounds;
1+
#version 300 es
32

4-
uniform highp mat4 perspective;
3+
precision highp float;
4+
precision highp int;
55

6-
attribute vec2 loc;
7-
attribute float size;
8-
attribute float tileID; // id
6+
uniform uvec2 sheetBounds;
97

10-
varying vec2 sheetAt;
8+
uniform mat4 perspective;
119

12-
vec2 intmodiv(float a, float b) {
13-
float m = a - floor((a + 0.5) / b) * b;
14-
return vec2(floor(m + 0.5), floor(a / b));
15-
}
10+
in vec2 loc;
11+
in float size;
12+
in uint tileID;
13+
14+
out vec2 sheetAt;
1615

1716
void main(void) {
1817
gl_Position = perspective * vec4(
@@ -23,5 +22,7 @@ void main(void) {
2322

2423
gl_PointSize = size;
2524

26-
sheetAt = intmodiv(tileID, sheetBounds.x);
25+
sheetAt = vec2(
26+
tileID % sheetBounds.x,
27+
tileID / sheetBounds.x);
2728
}

0 commit comments

Comments
 (0)