-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl-tile-test.js
154 lines (132 loc) · 5.53 KB
/
webgl-tile-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
async function runTileUpProgram(gl, inputTexture, tiledTexture, originalWidth, originalHeight, tileLength, tiledWidth, tiledHeight) {
const fragmentShaderSource = `
precision highp float;
precision mediump int;
varying vec2 TexCoords;
uniform sampler2D A;
uniform int tileLength;
uniform int tiledHeight;
uniform int tiledWidth;
uniform int height;
uniform int width;
${glslOffsetToCoords()}
${glslCoordsToOffset()}
${glslTiledCoordsToOriginal()}
void main() {
vec2 coords = tiledCoordsToOriginal(TexCoords, tiledWidth, tiledHeight, tileLength, width, height);
gl_FragColor = texture2D(A, coords);
}`;
const program = createProgram(gl, getDefaultVertexShader(gl),
compileShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER));
const hA = gl.getUniformLocation(program, 'A');
const hTileLength = gl.getUniformLocation(program, 'tileLength');
const hTiledHeight = gl.getUniformLocation(program, 'tiledHeight');
const hTiledWidth = gl.getUniformLocation(program, 'tiledWidth');
const hHeight = gl.getUniformLocation(program, 'height');
const hWidth = gl.getUniformLocation(program, 'width');
gl.useProgram(program);
attachOutputTexture(gl, tiledTexture);
gl.viewport(0, 0, tiledWidth, tiledHeight);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, inputTexture);
gl.uniform1i(hA, 0);
gl.uniform1i(hTileLength, tileLength);
gl.uniform1i(hTiledHeight, tiledHeight);
gl.uniform1i(hTiledWidth, tiledWidth);
gl.uniform1i(hHeight, originalHeight);
gl.uniform1i(hWidth, originalWidth);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
await waitForSync(gl);
};
async function runDetileProgram(gl, tiledTexture, originalTexture, originalWidth, originalHeight, tileLength, tiledWidth, tiledHeight) {
const fragmentShaderSource = `
precision highp float;
precision mediump int;
varying vec2 TexCoords;
uniform sampler2D A;
uniform int tileLength;
uniform int tiledHeight;
uniform int tiledWidth;
uniform int height;
uniform int width;
${glslOffsetToCoords()}
${glslCoordsToOffset()}
${glslOriginalCoordsToTiled()}
void main() {
vec2 coords = originalCoordsToTiled(TexCoords, width, height, tileLength, tiledWidth, tiledHeight);
gl_FragColor = texture2D(A, coords);
}`;
const program = createProgram(gl, getDefaultVertexShader(gl),
compileShader(gl, fragmentShaderSource, gl.FRAGMENT_SHADER));
const hA = gl.getUniformLocation(program, 'A');
const hTileLength = gl.getUniformLocation(program, 'tileLength');
const hTiledHeight = gl.getUniformLocation(program, 'tiledHeight');
const hTiledWidth = gl.getUniformLocation(program, 'tiledWidth');
const hHeight = gl.getUniformLocation(program, 'height');
const hWidth = gl.getUniformLocation(program, 'width');
gl.useProgram(program);
attachOutputTexture(gl, originalTexture);
gl.viewport(0, 0, originalWidth, originalHeight);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, tiledTexture);
gl.uniform1i(hA, 0);
gl.uniform1i(hTileLength, tileLength);
gl.uniform1i(hTiledHeight, tiledHeight);
gl.uniform1i(hTiledWidth, tiledWidth);
gl.uniform1i(hHeight, originalHeight);
gl.uniform1i(hWidth, originalWidth);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
await waitForSync(gl);
};
function calcTiledTextureShape(shape, pivot, tileLength) {
const leftDimsLength = shape.slice(0, pivot);
const rightDimsLength = shape.slice(pivot).reduce((a,b)=>a*b);
const width = Math.ceil(rightDimsLength/tileLength);
const height = leftDimsLength * tileLength;
return [ width, height ];
}
function getTestData() {
return [
{ shape:[3,12]}
];
}
//
// Main
//
async function main() {
const canvas = createCanvas(1, 1);
const gl = getContext(canvas);
setupVBO(gl);
createFrameBuffer(gl);
const testDatas = getTestData();
for(let i = 0; i < testDatas.length; ++i) {
const testData = testDatas[i];
const inputShape = testData.shape;
const tileLength = 2;
const [tiledWidth, tiledHeight] = calcTiledTextureShape(inputShape, 1, tileLength);
console.log(`tiledWidth=${tiledWidth}, tiledHeight=${tiledHeight}`);
const input = new Float32Array(new Array(inputShape[0] * inputShape[1]).fill(0).map((v,i)=>i));
const inputTexture = createTexture(gl, gl.R32F, gl.RED, gl.FLOAT, inputShape[1], inputShape[0], input);
debugPrintTexture(gl, inputTexture, inputShape[1], inputShape[0], gl.RED, gl.FLOAT);
const tiledTexture = createTexture(gl, gl.R32F, gl.RED, gl.FLOAT, tiledWidth, tiledHeight, null);
await runTileUpProgram(gl, inputTexture, tiledTexture, inputShape[1], inputShape[0], tileLength, tiledWidth, tiledHeight);
debugPrintTexture(gl, tiledTexture, tiledWidth, tiledHeight, gl.RED, gl.FLOAT);
const originalWidth = inputShape[1];
const originalHeight = inputShape[0];
const originalTexture = createTexture(gl, gl.R32F, gl.RED, gl.FLOAT, originalWidth, originalHeight, null);
await runDetileProgram(gl, tiledTexture, originalTexture, originalWidth, originalHeight, tileLength, tiledWidth, tiledHeight);
const output = new Float32Array(originalWidth * originalHeight);
readOutput(gl, originalWidth, originalHeight, gl.RED, gl.FLOAT, output);
if(!compareOutputs(output, input, 0.1)) {
console.error('Expected and Actual did not match');
console.log(output);
console.log(input);
} else {
console.info('Actual and expected matched!');
}
gl.deleteTexture(inputTexture);
gl.deleteTexture(tiledTexture);
gl.deleteTexture(originalTexture);
}
}
main();