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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
/*
* Mesa 3-D graphics library
*
* Copyright (C) 2014 LunarG, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Authors:
* Chia-I Wu <olv@lunarg.com>
*/
#ifndef ILO_BUILDER_H
#define ILO_BUILDER_H
#include "core/intel_winsys.h"
#include "ilo_common.h"
enum ilo_builder_writer_type {
ILO_BUILDER_WRITER_BATCH,
ILO_BUILDER_WRITER_INSTRUCTION,
ILO_BUILDER_WRITER_COUNT,
};
enum ilo_builder_item_type {
/* for dynamic buffer */
ILO_BUILDER_ITEM_BLOB,
ILO_BUILDER_ITEM_CLIP_VIEWPORT,
ILO_BUILDER_ITEM_SF_VIEWPORT,
ILO_BUILDER_ITEM_SCISSOR_RECT,
ILO_BUILDER_ITEM_CC_VIEWPORT,
ILO_BUILDER_ITEM_COLOR_CALC,
ILO_BUILDER_ITEM_DEPTH_STENCIL,
ILO_BUILDER_ITEM_BLEND,
ILO_BUILDER_ITEM_SAMPLER,
ILO_BUILDER_ITEM_INTERFACE_DESCRIPTOR,
/* for surface buffer */
ILO_BUILDER_ITEM_SURFACE,
ILO_BUILDER_ITEM_BINDING_TABLE,
/* for instruction buffer */
ILO_BUILDER_ITEM_KERNEL,
ILO_BUILDER_ITEM_COUNT,
};
struct ilo_builder_item {
enum ilo_builder_item_type type;
unsigned offset;
unsigned size;
};
struct ilo_builder_writer {
/* internal flags */
unsigned flags;
unsigned size;
struct intel_bo *bo;
void *ptr;
/* data written to the bottom */
unsigned used;
/* data written to the top */
unsigned stolen;
/* for decoding */
struct ilo_builder_item *items;
unsigned item_alloc;
unsigned item_used;
};
/**
* A snapshot of the writer state.
*/
struct ilo_builder_snapshot {
unsigned reloc_count;
unsigned used;
unsigned stolen;
unsigned item_used;
};
struct ilo_builder {
const struct ilo_dev_info *dev;
struct intel_winsys *winsys;
uint32_t mocs;
struct ilo_builder_writer writers[ILO_BUILDER_WRITER_COUNT];
bool unrecoverable_error;
/* for writers that have their data appended */
unsigned begin_used[ILO_BUILDER_WRITER_COUNT];
/* for STATE_BASE_ADDRESS */
unsigned sba_instruction_pos;
};
void
ilo_builder_init(struct ilo_builder *builder,
const struct ilo_dev_info *dev,
struct intel_winsys *winsys);
void
ilo_builder_reset(struct ilo_builder *builder);
void
ilo_builder_decode(struct ilo_builder *builder);
bool
ilo_builder_begin(struct ilo_builder *builder);
struct intel_bo *
ilo_builder_end(struct ilo_builder *builder, unsigned *used);
bool
ilo_builder_validate(struct ilo_builder *builder,
unsigned bo_count, struct intel_bo **bos);
/**
* Return true if the builder has a relocation entry for \p bo.
*/
static inline bool
ilo_builder_has_reloc(const struct ilo_builder *builder,
struct intel_bo *bo)
{
int i;
for (i = 0; i < ILO_BUILDER_WRITER_COUNT; i++) {
const struct ilo_builder_writer *writer = &builder->writers[i];
if (intel_bo_has_reloc(writer->bo, bo))
return true;
}
return false;
}
void
ilo_builder_writer_discard(struct ilo_builder *builder,
enum ilo_builder_writer_type which);
bool
ilo_builder_writer_grow(struct ilo_builder *builder,
enum ilo_builder_writer_type which,
unsigned new_size, bool preserve);
bool
ilo_builder_writer_record(struct ilo_builder *builder,
enum ilo_builder_writer_type which,
enum ilo_builder_item_type type,
unsigned offset, unsigned size);
static inline void
ilo_builder_writer_checked_record(struct ilo_builder *builder,
enum ilo_builder_writer_type which,
enum ilo_builder_item_type item,
unsigned offset, unsigned size)
{
if (unlikely(ilo_debug & (ILO_DEBUG_BATCH | ILO_DEBUG_HANG))) {
if (!ilo_builder_writer_record(builder, which, item, offset, size)) {
builder->unrecoverable_error = true;
builder->writers[which].item_used = 0;
}
}
}
/**
* Return an offset to a region that is aligned to \p alignment and has at
* least \p size bytes. The region is reserved from the bottom.
*/
static inline unsigned
ilo_builder_writer_reserve_bottom(struct ilo_builder *builder,
enum ilo_builder_writer_type which,
unsigned alignment, unsigned size)
{
struct ilo_builder_writer *writer = &builder->writers[which];
unsigned offset;
assert(alignment && util_is_power_of_two(alignment));
offset = align(writer->used, alignment);
if (unlikely(offset + size > writer->size - writer->stolen)) {
if (!ilo_builder_writer_grow(builder, which,
offset + size + writer->stolen, true)) {
builder->unrecoverable_error = true;
ilo_builder_writer_discard(builder, which);
offset = 0;
}
assert(offset + size <= writer->size - writer->stolen);
}
return offset;
}
/**
* Similar to ilo_builder_writer_reserve_bottom(), but reserve from the top.
*/
static inline unsigned
ilo_builder_writer_reserve_top(struct ilo_builder *builder,
enum ilo_builder_writer_type which,
unsigned alignment, unsigned size)
{
struct ilo_builder_writer *writer = &builder->writers[which];
unsigned offset;
assert(alignment && util_is_power_of_two(alignment));
offset = (writer->size - writer->stolen - size) & ~(alignment - 1);
if (unlikely(offset < writer->used ||
size > writer->size - writer->stolen)) {
if (!ilo_builder_writer_grow(builder, which,
align(writer->used, alignment) + size + writer->stolen, true)) {
builder->unrecoverable_error = true;
ilo_builder_writer_discard(builder, which);
}
offset = (writer->size - writer->stolen - size) & ~(alignment - 1);
assert(offset + size <= writer->size - writer->stolen);
}
return offset;
}
/**
* Add a relocation entry to the writer.
*/
static inline void
ilo_builder_writer_reloc(struct ilo_builder *builder,
enum ilo_builder_writer_type which,
unsigned offset, struct intel_bo *bo,
unsigned bo_offset, unsigned reloc_flags,
bool write_presumed_offset_hi)
{
struct ilo_builder_writer *writer = &builder->writers[which];
uint64_t presumed_offset;
int err;
if (write_presumed_offset_hi)
ILO_DEV_ASSERT(builder->dev, 8, 8);
else
ILO_DEV_ASSERT(builder->dev, 6, 7.5);
assert(offset + sizeof(uint32_t) <= writer->used ||
(offset >= writer->size - writer->stolen &&
offset + sizeof(uint32_t) <= writer->size));
err = intel_bo_add_reloc(writer->bo, offset, bo, bo_offset,
reloc_flags, &presumed_offset);
if (unlikely(err))
builder->unrecoverable_error = true;
if (write_presumed_offset_hi) {
*((uint64_t *) ((char *) writer->ptr + offset)) = presumed_offset;
} else {
/* 32-bit addressing */
assert(presumed_offset == (uint64_t) ((uint32_t) presumed_offset));
*((uint32_t *) ((char *) writer->ptr + offset)) = presumed_offset;
}
}
/**
* Reserve a region from the dynamic buffer. Both the offset, in bytes, and
* the pointer to the reserved region are returned. The pointer is only valid
* until the next reserve call.
*
* Note that \p alignment is in bytes and \p len is in DWords.
*/
static inline uint32_t
ilo_builder_dynamic_pointer(struct ilo_builder *builder,
enum ilo_builder_item_type item,
unsigned alignment, unsigned len,
uint32_t **dw)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
const unsigned size = len << 2;
const unsigned offset = ilo_builder_writer_reserve_top(builder,
which, alignment, size);
struct ilo_builder_writer *writer = &builder->writers[which];
/* all states are at least aligned to 32-bytes */
if (item != ILO_BUILDER_ITEM_BLOB)
assert(alignment % 32 == 0);
*dw = (uint32_t *) ((char *) writer->ptr + offset);
writer->stolen = writer->size - offset;
ilo_builder_writer_checked_record(builder, which, item, offset, size);
return offset;
}
/**
* Write a dynamic state to the dynamic buffer.
*/
static inline uint32_t
ilo_builder_dynamic_write(struct ilo_builder *builder,
enum ilo_builder_item_type item,
unsigned alignment, unsigned len,
const uint32_t *dw)
{
uint32_t offset, *dst;
offset = ilo_builder_dynamic_pointer(builder, item, alignment, len, &dst);
memcpy(dst, dw, len << 2);
return offset;
}
/**
* Reserve some space from the top (for prefetches).
*/
static inline void
ilo_builder_dynamic_pad_top(struct ilo_builder *builder, unsigned len)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
const unsigned size = len << 2;
struct ilo_builder_writer *writer = &builder->writers[which];
if (writer->stolen < size) {
ilo_builder_writer_reserve_top(builder, which,
1, size - writer->stolen);
writer->stolen = size;
}
}
static inline unsigned
ilo_builder_dynamic_used(const struct ilo_builder *builder)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
const struct ilo_builder_writer *writer = &builder->writers[which];
return writer->stolen >> 2;
}
/**
* Reserve a region from the surface buffer. Both the offset, in bytes, and
* the pointer to the reserved region are returned. The pointer is only valid
* until the next reserve call.
*
* Note that \p alignment is in bytes and \p len is in DWords.
*/
static inline uint32_t
ilo_builder_surface_pointer(struct ilo_builder *builder,
enum ilo_builder_item_type item,
unsigned alignment, unsigned len,
uint32_t **dw)
{
assert(item == ILO_BUILDER_ITEM_SURFACE ||
item == ILO_BUILDER_ITEM_BINDING_TABLE);
return ilo_builder_dynamic_pointer(builder, item, alignment, len, dw);
}
/**
* Add a relocation entry for a DWord of a surface state.
*/
static inline void
ilo_builder_surface_reloc(struct ilo_builder *builder,
uint32_t offset, unsigned dw_index,
struct intel_bo *bo, unsigned bo_offset,
unsigned reloc_flags)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
ilo_builder_writer_reloc(builder, which, offset + (dw_index << 2),
bo, bo_offset, reloc_flags, false);
}
static inline void
ilo_builder_surface_reloc64(struct ilo_builder *builder,
uint32_t offset, unsigned dw_index,
struct intel_bo *bo, unsigned bo_offset,
unsigned reloc_flags)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
ilo_builder_writer_reloc(builder, which, offset + (dw_index << 2),
bo, bo_offset, reloc_flags, true);
}
static inline unsigned
ilo_builder_surface_used(const struct ilo_builder *builder)
{
return ilo_builder_dynamic_used(builder);
}
/**
* Write a kernel to the instruction buffer. The offset, in bytes, of the
* kernel is returned.
*/
static inline uint32_t
ilo_builder_instruction_write(struct ilo_builder *builder,
unsigned size, const void *kernel)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_INSTRUCTION;
/*
* From the Sandy Bridge PRM, volume 4 part 2, page 112:
*
* "Due to prefetch of the instruction stream, the EUs may attempt to
* access up to 8 instructions (128 bytes) beyond the end of the
* kernel program - possibly into the next memory page. Although
* these instructions will not be executed, software must account for
* the prefetch in order to avoid invalid page access faults."
*/
const unsigned reserved_size = size + 128;
/* kernels are aligned to 64 bytes */
const unsigned alignment = 64;
const unsigned offset = ilo_builder_writer_reserve_bottom(builder,
which, alignment, reserved_size);
struct ilo_builder_writer *writer = &builder->writers[which];
memcpy((char *) writer->ptr + offset, kernel, size);
writer->used = offset + size;
ilo_builder_writer_checked_record(builder, which,
ILO_BUILDER_ITEM_KERNEL, offset, size);
return offset;
}
/**
* Reserve a region from the batch buffer. Both the offset, in DWords, and
* the pointer to the reserved region are returned. The pointer is only valid
* until the next reserve call.
*
* Note that \p len is in DWords.
*/
static inline unsigned
ilo_builder_batch_pointer(struct ilo_builder *builder,
unsigned len, uint32_t **dw)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
/*
* We know the batch bo is always aligned. Using 1 here should allow the
* compiler to optimize away aligning.
*/
const unsigned alignment = 1;
const unsigned size = len << 2;
const unsigned offset = ilo_builder_writer_reserve_bottom(builder,
which, alignment, size);
struct ilo_builder_writer *writer = &builder->writers[which];
assert(offset % 4 == 0);
*dw = (uint32_t *) ((char *) writer->ptr + offset);
writer->used = offset + size;
return offset >> 2;
}
/**
* Write a command to the batch buffer.
*/
static inline unsigned
ilo_builder_batch_write(struct ilo_builder *builder,
unsigned len, const uint32_t *dw)
{
unsigned pos;
uint32_t *dst;
pos = ilo_builder_batch_pointer(builder, len, &dst);
memcpy(dst, dw, len << 2);
return pos;
}
/**
* Add a relocation entry for a DWord of a command.
*/
static inline void
ilo_builder_batch_reloc(struct ilo_builder *builder, unsigned pos,
struct intel_bo *bo, unsigned bo_offset,
unsigned reloc_flags)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
ilo_builder_writer_reloc(builder, which, pos << 2,
bo, bo_offset, reloc_flags, false);
}
static inline void
ilo_builder_batch_reloc64(struct ilo_builder *builder, unsigned pos,
struct intel_bo *bo, unsigned bo_offset,
unsigned reloc_flags)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
ilo_builder_writer_reloc(builder, which, pos << 2,
bo, bo_offset, reloc_flags, true);
}
static inline unsigned
ilo_builder_batch_used(const struct ilo_builder *builder)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
const struct ilo_builder_writer *writer = &builder->writers[which];
return writer->used >> 2;
}
static inline unsigned
ilo_builder_batch_space(const struct ilo_builder *builder)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
const struct ilo_builder_writer *writer = &builder->writers[which];
return (writer->size - writer->stolen - writer->used) >> 2;
}
static inline void
ilo_builder_batch_discard(struct ilo_builder *builder)
{
ilo_builder_writer_discard(builder, ILO_BUILDER_WRITER_BATCH);
}
static inline void
ilo_builder_batch_print_stats(const struct ilo_builder *builder)
{
const enum ilo_builder_writer_type which = ILO_BUILDER_WRITER_BATCH;
const struct ilo_builder_writer *writer = &builder->writers[which];
ilo_printf("%d+%d bytes (%d%% full)\n",
writer->used, writer->stolen,
(writer->used + writer->stolen) * 100 / writer->size);
}
void
ilo_builder_batch_snapshot(const struct ilo_builder *builder,
struct ilo_builder_snapshot *snapshot);
void
ilo_builder_batch_restore(struct ilo_builder *builder,
const struct ilo_builder_snapshot *snapshot);
#endif /* ILO_BUILDER_H */
|