-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.cpp
341 lines (278 loc) · 9.59 KB
/
test.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include "mailbox.h"
#include "v3d.h"
// I/O access
volatile unsigned *v3d;
int mbox;
// Execute a nop control list to prove that we have contol.
void testControlLists() {
// First we allocate and map some videocore memory to build our control list in.
// ask the blob to allocate us 256 bytes with 4k alignment, zero it.
unsigned int handle = mem_alloc(mbox, 0x100, 0x1000, MEM_FLAG_COHERENT | MEM_FLAG_ZERO);
if (!handle) {
printf("Error: Unable to allocate memory");
return;
}
// ask the blob to lock our memory at a single location at give is that address.
uint32_t bus_addr = mem_lock(mbox, handle);
// map that address into our local address space.
uint8_t *list = (uint8_t*) mapmem(bus_addr, 0x100);
// Now we construct our control list.
// 255 nops, with a halt somewhere in the middle
for(int i = 0; i < 0xff; i++) {
list[i] = 1; // NOP
}
list[0xbb] = 0; // Halt.
// And then we setup the v3d pipeline to execute the control list.
printf("V3D_CT0CS: 0x%08x\n", v3d[V3D_CT0CS]);
printf("Start Address: 0x%08x\n", bus_addr);
// Current Address = start of our list (bus address)
v3d[V3D_CT0CA] = bus_addr;
// End Address = just after the end of our list (bus address)
// This also starts execution.
v3d[V3D_CT0EA] = bus_addr + 0x100;
// print status while running
printf("V3D_CT0CS: 0x%08x, Address: 0x%08x\n", v3d[V3D_CT0CS], v3d[V3D_CT0CA]);
// Wait a second to be sure the contorl list execution has finished
while(v3d[V3D_CT0CS] & 0x20);
// print the status again.
printf("V3D_CT0CS: 0x%08x, Address: 0x%08x\n", v3d[V3D_CT0CS], v3d[V3D_CT0CA]);
// Release memory;
unmapmem((void *) list, 0x100);
mem_unlock(mbox, handle);
mem_free(mbox, handle);
}
void addbyte(uint8_t **list, uint8_t d) {
*((*list)++) = d;
}
void addshort(uint8_t **list, uint16_t d) {
*((*list)++) = (d) & 0xff;
*((*list)++) = (d >> 8) & 0xff;
}
void addword(uint8_t **list, uint32_t d) {
*((*list)++) = (d) & 0xff;
*((*list)++) = (d >> 8) & 0xff;
*((*list)++) = (d >> 16) & 0xff;
*((*list)++) = (d >> 24) & 0xff;
}
void addfloat(uint8_t **list, float f) {
uint32_t d = *((uint32_t *)&f);
*((*list)++) = (d) & 0xff;
*((*list)++) = (d >> 8) & 0xff;
*((*list)++) = (d >> 16) & 0xff;
*((*list)++) = (d >> 24) & 0xff;
}
// Render a single triangle to memory.
void testTriangle() {
// Like above, we allocate/lock/map some videocore memory
// I'm just shoving everything in a single buffer because I'm lazy
// 8Mb, 4k alignment
unsigned int handle = mem_alloc(mbox, 0x800000, 0x1000, MEM_FLAG_COHERENT | MEM_FLAG_ZERO);
if (!handle) {
printf("Error: Unable to allocate memory");
return;
}
uint32_t bus_addr = mem_lock(mbox, handle);
uint8_t *list = (uint8_t*) mapmem(bus_addr, 0x800000);
uint8_t *p = list;
// Configuration stuff
// Tile Binning Configuration.
// Tile state data is 48 bytes per tile, I think it can be thrown away
// as soon as binning is finished.
addbyte(&p, 112);
addword(&p, bus_addr + 0x6200); // tile allocation memory address
addword(&p, 0x8000); // tile allocation memory size
addword(&p, bus_addr + 0x100); // Tile state data address
addbyte(&p, 30); // 1920/64
addbyte(&p, 17); // 1080/64 (16.875)
addbyte(&p, 0x04); // config
// Start tile binning.
addbyte(&p, 6);
// Primitive type
addbyte(&p, 56);
addbyte(&p, 0x32); // 16 bit triangle
// Clip Window
addbyte(&p, 102);
addshort(&p, 0);
addshort(&p, 0);
addshort(&p, 1920); // width
addshort(&p, 1080); // height
// State
addbyte(&p, 96);
addbyte(&p, 0x03); // enable both foward and back facing polygons
addbyte(&p, 0x00); // depth testing disabled
addbyte(&p, 0x02); // enable early depth write
// Viewport offset
addbyte(&p, 103);
addshort(&p, 0);
addshort(&p, 0);
// The triangle
// No Vertex Shader state (takes pre-transformed vertexes,
// so we don't have to supply a working coordinate shader to test the binner.
addbyte(&p, 65);
addword(&p, bus_addr + 0x80); // Shader Record
// primitive index list
addbyte(&p, 32);
addbyte(&p, 0x04); // 8bit index, trinagles
addword(&p, 3); // Length
addword(&p, bus_addr + 0x70); // address
addword(&p, 2); // Maximum index
// End of bin list
// Flush
addbyte(&p, 5);
// Nop
addbyte(&p, 1);
// Halt
addbyte(&p, 0);
int length = p - list;
assert(length < 0x80);
// Shader Record
p = list + 0x80;
addbyte(&p, 0x01); // flags
addbyte(&p, 6*4); // stride
addbyte(&p, 0xcc); // num uniforms (not used)
addbyte(&p, 3); // num varyings
addword(&p, bus_addr + 0xfe00); // Fragment shader code
addword(&p, bus_addr + 0xff00); // Fragment shader uniforms
addword(&p, bus_addr + 0xa0); // Vertex Data
// Vertex Data
p = list + 0xa0;
// Vertex: Top, red
addshort(&p, (1920/2) << 4); // X in 12.4 fixed point
addshort(&p, 200 << 4); // Y in 12.4 fixed point
addfloat(&p, 1.0f); // Z
addfloat(&p, 1.0f); // 1/W
addfloat(&p, 1.0f); // Varying 0 (Red)
addfloat(&p, 0.0f); // Varying 1 (Green)
addfloat(&p, 0.0f); // Varying 2 (Blue)
// Vertex: bottom left, Green
addshort(&p, 560 << 4); // X in 12.4 fixed point
addshort(&p, 800 << 4); // Y in 12.4 fixed point
addfloat(&p, 1.0f); // Z
addfloat(&p, 1.0f); // 1/W
addfloat(&p, 0.0f); // Varying 0 (Red)
addfloat(&p, 1.0f); // Varying 1 (Green)
addfloat(&p, 0.0f); // Varying 2 (Blue)
// Vertex: bottom right, Blue
addshort(&p, 1360 << 4); // X in 12.4 fixed point
addshort(&p, 800 << 4); // Y in 12.4 fixed point
addfloat(&p, 1.0f); // Z
addfloat(&p, 1.0f); // 1/W
addfloat(&p, 0.0f); // Varying 0 (Red)
addfloat(&p, 0.0f); // Varying 1 (Green)
addfloat(&p, 1.0f); // Varying 2 (Blue)
// Vertex list
p = list + 0x70;
addbyte(&p, 0); // top
addbyte(&p, 1); // bottom left
addbyte(&p, 2); // bottom right
// fragment shader
p = list + 0xfe00;
addword(&p, 0x958e0dbf);
addword(&p, 0xd1724823); /* mov r0, vary; mov r3.8d, 1.0 */
addword(&p, 0x818e7176);
addword(&p, 0x40024821); /* fadd r0, r0, r5; mov r1, vary */
addword(&p, 0x818e7376);
addword(&p, 0x10024862); /* fadd r1, r1, r5; mov r2, vary */
addword(&p, 0x819e7540);
addword(&p, 0x114248a3); /* fadd r2, r2, r5; mov r3.8a, r0 */
addword(&p, 0x809e7009);
addword(&p, 0x115049e3); /* nop; mov r3.8b, r1 */
addword(&p, 0x809e7012);
addword(&p, 0x116049e3); /* nop; mov r3.8c, r2 */
addword(&p, 0x159e76c0);
addword(&p, 0x30020ba7); /* mov tlbc, r3; nop; thrend */
addword(&p, 0x009e7000);
addword(&p, 0x100009e7); /* nop; nop; nop */
addword(&p, 0x009e7000);
addword(&p, 0x500009e7); /* nop; nop; sbdone */
// Render control list
p = list + 0xe200;
// Clear color
addbyte(&p, 114);
addword(&p, 0xff000000); // Opaque Black
addword(&p, 0xff000000); // 32 bit clear colours need to be repeated twice
addword(&p, 0);
addbyte(&p, 0);
// Tile Rendering Mode Configuration
addbyte(&p, 113);
addword(&p, bus_addr + 0x10000); // framebuffer addresss
addshort(&p, 1920); // width
addshort(&p, 1080); // height
addbyte(&p, 0x04); // framebuffer mode (linear rgba8888)
addbyte(&p, 0x00);
// Do a store of the first tile to force the tile buffer to be cleared
// Tile Coordinates
addbyte(&p, 115);
addbyte(&p, 0);
addbyte(&p, 0);
// Store Tile Buffer General
addbyte(&p, 28);
addshort(&p, 0); // Store nothing (just clear)
addword(&p, 0); // no address is needed
// Link all binned lists together
for(int x = 0; x < 30; x++) {
for(int y = 0; y < 17; y++) {
// Tile Coordinates
addbyte(&p, 115);
addbyte(&p, x);
addbyte(&p, y);
// Call Tile sublist
addbyte(&p, 17);
addword(&p, bus_addr + 0x6200 + (y * 30 + x) * 32);
// Last tile needs a special store instruction
if(x == 29 && y == 16) {
// Store resolved tile color buffer and signal end of frame
addbyte(&p, 25);
} else {
// Store resolved tile color buffer
addbyte(&p, 24);
}
}
}
int render_length = p - (list + 0xe200);
// Run our control list
printf("Binner control list constructed\n");
printf("Start Address: 0x%08x, length: 0x%x\n", bus_addr, length);
v3d[V3D_CT0CA] = bus_addr;
v3d[V3D_CT0EA] = bus_addr + length;
printf("V3D_CT0CS: 0x%08x, Address: 0x%08x\n", v3d[V3D_CT0CS], v3d[V3D_CT0CA]);
// Wait for control list to execute
while(v3d[V3D_CT0CS] & 0x20);
printf("V3D_CT0CS: 0x%08x, Address: 0x%08x\n", v3d[V3D_CT0CS], v3d[V3D_CT0CA]);
printf("V3D_CT1CS: 0x%08x, Address: 0x%08x\n", v3d[V3D_CT1CS], v3d[V3D_CT1CA]);
v3d[V3D_CT1CA] = bus_addr + 0xe200;
v3d[V3D_CT1EA] = bus_addr + 0xe200 + render_length;
while(v3d[V3D_CT1CS] & 0x20);
printf("V3D_CT1CS: 0x%08x, Address: 0x%08x\n", v3d[V3D_CT1CS], v3d[V3D_CT1CA]);
v3d[V3D_CT1CS] = 0x20;
// just dump the frame to a file
FILE *f = fopen("frame.data", "w");
fwrite(list + 0x10000, (1920*1080*4), 1, f);
fclose(f);
printf("frame buffer memory dumpped to frame.data\n");
// Release resources
unmapmem((void *) list, 0x800000);
mem_unlock(mbox, handle);
mem_free(mbox, handle);
}
int main(int argc, char **argv) {
mbox = mbox_open();
// The blob now has this nice handy call which powers up the v3d pipeline.
qpu_enable(mbox, 1);
// map v3d's registers into our address space.
v3d = (unsigned *) mapmem(0x20c00000, 0x1000);
if(v3d[V3D_IDENT0] != 0x02443356) { // Magic number.
printf("Error: V3D pipeline isn't powered up and accessable.\n");
exit(-1);
}
// We now have access to the v3d registers, we should do something.
testTriangle();
return 0;
}