Skip to content

Commit 478d592

Browse files
committed
Add some concept of grouped shader data and buffers
1 parent e98c100 commit 478d592

File tree

6 files changed

+212
-79
lines changed

6 files changed

+212
-79
lines changed

build_web.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ tsFiles='
99
web/math.ts
1010
web/shaders.ts
1111
web/shaders_color.ts
12+
web/shaders_texture.ts
1213
web/index.ts
1314
'
1415
tsc $tsFiles --outFile build/index.js --lib dom,es2015

wasm/gunfight.c

Lines changed: 94 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#include "gunfight.h"
22

3-
// ENV IMPORTS
4-
void logFloat32(f32 val);
5-
6-
extern u8 *__heap_base;
7-
83
// NOTE(fede): We define everything statically here because we already have all the memory
94
// we will use (it is allocated and passed from js as a WebAssembly.Memory object.
105
// Defining variables used globally in the environment seems not worth it here
@@ -13,35 +8,98 @@ extern u8 *__heap_base;
138
// GLOBAL GAME STATE
149
global_variable bool32 globalIsInitialized = 0;
1510
global_variable f64 globalLastTimestamp;
11+
1612
global_variable GameState globalGameState;
17-
global_variable GameControllerInput globalGameControllerInput = {};
18-
global_variable GameControllerInput globalLastFrameControllerInput = {};
13+
14+
global_variable GameControllerInput globalGameControllerInputCurrent = {};
15+
global_variable GameControllerInput globalGameControllerInputLastFrame = {};
16+
17+
global_variable u32 globalColorShaderTrianglesCount = 0;
18+
global_variable u32 globalTextureShaderTrianglesCount = 0;
1919

2020
// GLOBAL BUFFERS
21-
// 4 f32 (4 bytes) per vertex, 3 vertices per triangle, 100 triangles
22-
// 4 * 4 * 3 * 100 = 4800 bytes =~ 4.6KB
23-
global_variable u32 globalVertexBufferSize = 4800;
24-
// 4 f32 (4 bytes) per color, 100 triangles
25-
// 4 * 4 * 100 = 1600 bytes =~ 1.6KB
26-
global_variable u32 globalColorBufferSize = 1600;
2721

28-
// GLOBAL FRAME STATE
29-
global_variable u32 globalTrianglesCount = 0;
3022

31-
export u8 *getBufferBase() {
23+
// Define indices for each buffer
24+
enum bufferIndex {INDEX_COLOR_SHADER_VERTICES, INDEX_COLOR_SHADER_COLORS, INDEX_TEXTURE_SHADER_VERTICES};
25+
26+
global_variable u32 globalBufferSizes[3] = {
27+
// ColorShaderVertices: 4 f32 (4 bytes) per vertex, 3 vertices per triangle, 100 triangles
28+
4800,
29+
// ColorShaderColor: 4 f32 (4 bytes) per color, 100 triangles
30+
1600,
31+
// TextureShaderVertices: 4 f32 (4 bytes) per vertex, 3 vertices per triangle, 100 triangles
32+
4800
33+
};
34+
35+
36+
// GLOBAL FRAME STATE
37+
export u8 *getHeapBase() {
3238
return __heap_base;
3339
}
3440

41+
export u8 *getBufferBase(index) {
42+
u8 *ret = __heap_base;
43+
44+
assert(index < arrayLength(globalBufferSizes));
45+
46+
for(int i = 0; i < index; i++) {
47+
ret = ret + globalBufferSizes[i];
48+
}
49+
return ret;
50+
}
51+
52+
export u32 colorShaderGetTrianglesCount() {
53+
return globalColorShaderTrianglesCount;
54+
}
55+
56+
internal
57+
void bufferPushf32(Buffer *buffer, f32 value) {
58+
*((f32 *)(buffer->current + buffer->offset)) = value;
59+
buffer->offset += sizeof(f32);
60+
}
61+
62+
ColorShaderFrame colorShaderFrameInit() {
63+
ColorShaderFrame ret = {};
64+
ret.trianglesCount = 0;
65+
66+
Buffer verticesBuffer = {};
67+
verticesBuffer.current = getBufferBase(INDEX_COLOR_SHADER_VERTICES);
68+
verticesBuffer.offset = 0;
69+
70+
ret.verticesBuffer = verticesBuffer;
71+
72+
Buffer colorsBuffer = {};
73+
colorsBuffer.current = getBufferBase(INDEX_COLOR_SHADER_COLORS);
74+
colorsBuffer.offset = 0;
75+
76+
ret.colorsBuffer = colorsBuffer;
77+
78+
return ret;
79+
}
80+
81+
TextureShaderFrame textureShaderFrameInit() {
82+
TextureShaderFrame ret = {};
83+
ret.trianglesCount = 0;
84+
85+
Buffer verticesBuffer = {};
86+
verticesBuffer.current = getBufferBase(INDEX_COLOR_SHADER_VERTICES);
87+
verticesBuffer.offset = 0;
88+
89+
ret.verticesBuffer = verticesBuffer;
90+
91+
return ret;
92+
}
93+
3594
// CONTROLLER
3695
export void processControllerInput(u32 keyIndex, bool32 isDown) {
37-
GameButtonState *buttonState = &globalGameControllerInput.buttons[keyIndex];
96+
GameButtonState *buttonState = &globalGameControllerInputCurrent.buttons[keyIndex];
3897
buttonState->isDown = isDown;
3998
}
4099

41100
// DRAW
42101
internal void drawRectangle(
43-
Buffer *vertexBuffer,
44-
Buffer *colorBuffer,
102+
ColorShaderFrame *colorShaderFrame,
45103
Color color,
46104
f32 minX,
47105
f32 minY,
@@ -57,54 +115,29 @@ internal void drawRectangle(
57115
maxX, maxY,
58116
};
59117

60-
f32 *vertexPointer = (f32 *)vertexBuffer->current;
61-
f32 *colorPointer = (f32 *)colorBuffer->current;
118+
Buffer *verticesBuffer = &colorShaderFrame->verticesBuffer;
119+
Buffer *colorsBuffer = &colorShaderFrame->colorsBuffer;
62120

63121
for(int i = 0; i < arrayLength(vertices); i++) {
64-
*vertexPointer++ = vertices[i];
122+
bufferPushf32(verticesBuffer, vertices[i]);
65123
}
66124

67125
// every three vertices pairs (triangle) we need to specify color and increment triangles
68126
// count
69127
for(int i = 0; i < arrayLength(vertices)/6; i++) {
70-
*colorPointer++ = color.r;
71-
*colorPointer++ = color.g;
72-
*colorPointer++ = color.b;
73-
*colorPointer++ = color.a;
128+
bufferPushf32(colorsBuffer, color.r);
129+
bufferPushf32(colorsBuffer, color.g);
130+
bufferPushf32(colorsBuffer, color.b);
131+
bufferPushf32(colorsBuffer, color.a);
74132

75-
++globalTrianglesCount;
133+
colorShaderFrame->trianglesCount++;
76134
}
77-
78-
vertexBuffer->current = (u8 *)vertexPointer;
79-
colorBuffer->current = (u8 *)colorPointer;
80-
81-
82-
}
83-
84-
85-
// NOTE(fede)
86-
// from base we store in order:
87-
// 1) vertices
88-
// 2) colors
89-
export u8 *getVertexBufferBase() {
90-
return __heap_base;
91135
}
92136

93-
export u8 *getColorBufferBase() {
94-
return (__heap_base + globalVertexBufferSize);
95-
}
96-
97-
export u32 getTrianglesCount() {
98-
return globalTrianglesCount;
99-
}
100137

101138
// RENDER
102-
internal void resetFrameState() {
103-
globalTrianglesCount = 0;
104-
}
105139

106140
export void updateAndRender(f64 timestamp) {
107-
resetFrameState();
108141
//TODO(fede): This values should be more dynamic. Also how to achieve this while
109142
// preserving aspect ratio?
110143
f32 levelHeightInPixels = 960;
@@ -131,16 +164,16 @@ export void updateAndRender(f64 timestamp) {
131164
f32 vx = 0.0f; // meters per second
132165
f32 vy = 0.0f;
133166

134-
if(globalGameControllerInput.moveUp.isDown) {
167+
if(globalGameControllerInputCurrent.moveUp.isDown) {
135168
vy = 1.5f;
136169
}
137-
if(globalGameControllerInput.moveDown.isDown) {
170+
if(globalGameControllerInputCurrent.moveDown.isDown) {
138171
vy = -1.5f;
139172
}
140-
if(globalGameControllerInput.moveRight.isDown) {
173+
if(globalGameControllerInputCurrent.moveRight.isDown) {
141174
vx = 1.5f;
142175
}
143-
if(globalGameControllerInput.moveLeft.isDown) {
176+
if(globalGameControllerInputCurrent.moveLeft.isDown) {
144177
vx = -1.5f;
145178
}
146179

@@ -168,8 +201,8 @@ export void updateAndRender(f64 timestamp) {
168201
globalGameState.playerPosition.y = playerMinY;
169202
}
170203

171-
Buffer verticesBuffer = { getVertexBufferBase(), getVertexBufferBase() };
172-
Buffer colorsBuffer = { getColorBufferBase(), getColorBufferBase() };
204+
//TODO: initialize shader frames
205+
ColorShaderFrame colorShaderFrame = colorShaderFrameInit();
173206

174207
Position positionInPixels;
175208
positionInPixels.x = globalGameState.playerPosition.x * metersToPixels;
@@ -187,8 +220,7 @@ export void updateAndRender(f64 timestamp) {
187220
color.a = 1.0f;
188221

189222
drawRectangle(
190-
&verticesBuffer,
191-
&colorsBuffer,
223+
&colorShaderFrame,
192224
color,
193225
minX,
194226
minY,
@@ -197,5 +229,7 @@ export void updateAndRender(f64 timestamp) {
197229
);
198230

199231

232+
globalColorShaderTrianglesCount = colorShaderFrame.trianglesCount;
200233
globalLastTimestamp = timestamp;
234+
globalGameControllerInputLastFrame = globalGameControllerInputCurrent;
201235
}

wasm/gunfight.h

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,23 @@ typedef long bool32;
1818
#define megabytes(value) (kilobytes(value)*1024LL)
1919
#define gigabytes(value) (megabytes(value)*1024LL)
2020

21-
#define assert(expression) if(!expression) { __builtin_trap(); }
21+
#define assert(expression) if(!(expression)) { __builtin_trap(); }
2222

23+
#include "gunfight_imports.h"
2324

25+
26+
// GAME STATE
2427
typedef struct Position {
2528
f32 x;
2629
f32 y;
2730
} Position;
2831

29-
typedef struct Color {
30-
f32 r;
31-
f32 g;
32-
f32 b;
33-
f32 a;
34-
} Color;
35-
36-
typedef struct Buffer {
37-
u8 *current;
38-
u8 *base;
39-
} Buffer;
40-
4132
typedef struct GameState {
4233
Position playerPosition;
4334
} GameState;
4435

36+
37+
// CONTROLLER
4538
typedef struct GameButtonState
4639
{
4740
bool32 isDown;
@@ -62,3 +55,32 @@ typedef struct GameControllerInput
6255
};
6356
};
6457
} GameControllerInput;
58+
59+
// BUFFERS
60+
61+
typedef struct Buffer {
62+
u8 *current;
63+
u32 offset;
64+
} Buffer;
65+
66+
// RENDERING
67+
68+
// Data about what the color shader should draw on the next frame
69+
typedef struct ColorShaderFrame {
70+
u32 trianglesCount;
71+
Buffer verticesBuffer;
72+
Buffer colorsBuffer;
73+
} ColorShaderFrame;
74+
75+
// Data for the texture shader to draw on the current frame
76+
typedef struct TextureShaderFrame {
77+
u32 trianglesCount;
78+
Buffer verticesBuffer;
79+
} TextureShaderFrame;
80+
81+
typedef struct Color {
82+
f32 r;
83+
f32 g;
84+
f32 b;
85+
f32 a;
86+
} Color;

wasm/gunfight_imports.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#if !defined(GUNFIGHT_IMPORTS)
2+
3+
// ENV IMPORTS
4+
void logFloat32(f32 val);
5+
6+
extern u8 *__heap_base;
7+
8+
#define GUNFIGHT_IMPORTS
9+
#endif

web/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function waitForInitialization() {
4949
}
5050

5151
function main(context) {
52-
console.log(context);
5352
let wasm = context.wasm;
5453
// TODO: draw the triangle
5554
let canvas = <HTMLCanvasElement> document.getElementById('canvas');
@@ -64,6 +63,9 @@ function main(context) {
6463

6564
// Setup Shaders
6665
let colorShaderInfo = colorShaderSetup(gl);
66+
let textureShaderInfo = textureShaderSetup(gl, context.images[0]);
67+
68+
console.log(textureShaderInfo);
6769

6870

6971
// Setup event listeners
@@ -140,27 +142,29 @@ function run(wasm, gl, colorShaderInfo) {
140142
let matrixProjection = Mat3.projection(gl.canvas.width, gl.canvas.height);
141143
gl.uniformMatrix3fv(colorShaderInfo.locations.uMatrix, false, matrixProjection);
142144

143-
let numberOfTriangles = wasm.instance.exports.getTrianglesCount();
145+
let numberOfTriangles = wasm.instance.exports.colorShaderGetTrianglesCount();
146+
console.log(numberOfTriangles);
144147
let numberOfVertices = numberOfTriangles * 3;
145148
let pointsPerVertex = 2;
146149
let bytesPerFloat32 = 4;
147150

148151
// Add vertices to array buffer
149-
let vertexBufferBase = wasm.instance.exports.getVertexBufferBase();
152+
let vertexBufferBase = wasm.instance.exports.getBufferBase(0);
150153
let vertexSlice =
151154
wasmMemory.buffer.slice(
152155
vertexBufferBase,
153156
vertexBufferBase + numberOfVertices * pointsPerVertex * bytesPerFloat32
154157
);
158+
console.log(vertexSlice);
155159
let vertexBuffer = new Float32Array(vertexSlice);
156160
gl.bufferData(gl.ARRAY_BUFFER, vertexBuffer, gl.STATIC_DRAW);
157161

158-
let colorBufferUnitSize = 4 * bytesPerFloat32; // 4 f32s pre color
159-
let colorBufferBase = wasm.instance.exports.getColorBufferBase();
162+
let colorBufferUnitSize = 4 * bytesPerFloat32; // 4 f32s per color
163+
let colorBufferBase = wasm.instance.exports.getBufferBase(1);
160164
let colorSlice =
161165
wasmMemory.buffer.slice(
162166
colorBufferBase,
163-
colorBufferBase + numberOfTriangles * colorBufferUnitSize // 4 float32s per color
167+
colorBufferBase + numberOfTriangles * colorBufferUnitSize
164168
);
165169
let colorArray = new Float32Array(colorSlice);
166170

0 commit comments

Comments
 (0)