Debian Codegraph @

src/panfrost/lib/pan_samples.c

raw
1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <stdint.h>
7
8 #include "pan_samples.h"
9
10 /* Sample positions are specified partially in hardware, partially in software
11 * on Mali. On Midgard, sample positions are completely fixed but need to be
12 * software accessible to implement gl_SamplePosition. On Bifrost, sample
13 * positions are part fixed, part programmable, and again need to be accessible
14 * for gl_SamplePosition. The mali_sample_position data structure is
15 * hardware-defined on Bifrost as a packed 8:8 fixed point format. The
16 * mali_sample_positions array is part-hardware, part-software-defined: it must
17 * give the sample position when indexed by the sample index, as well as
18 * reading back the origin when indexed by 32.
19 *
20 * The upshot is all of this is known at compile-time, so we can just hardcode
21 * the LUT, upload it to GPU memory on device initialization, and forget about
22 * it.
23 */
24
25 struct mali_sample_position {
26 uint16_t x, y;
27 } __attribute__((packed));
28
29 struct mali_sample_positions {
30 struct mali_sample_position positions[32];
31 struct mali_sample_position origin;
32 struct mali_sample_position padding[64 - (32 + 1)];
33 } __attribute__((packed));
34
35 /* SAMPLE16 constructs a single sample in terms of 1/16's of the grid, centered
36 * at the origin. SAMPLE4/8 swap the units for legibility. */
37
38 #define SAMPLE16(x, y) \
39 { \
40 (((x) + 8) * (256 / 16)), (((y) + 8) * (256 / 16)) \
41 }
42
43 #define SAMPLE8(x, y) SAMPLE16((x)*2, (y)*2)
44 #define SAMPLE4(x, y) SAMPLE16((x)*4, (y)*4)
45
46 /* clang-format off */
47 const struct mali_sample_positions sample_position_lut[] = {
48 [MALI_SAMPLE_PATTERN_SINGLE_SAMPLED] = {
49 .positions = {
50 SAMPLE4(0, 0)
51 },
52 .origin = SAMPLE4(0, 0)
53 },
54
55 [MALI_SAMPLE_PATTERN_ORDERED_4X_GRID] = {
56 .positions = {
57 SAMPLE4(-1, -1),
58 SAMPLE4( 1, -1),
59 SAMPLE4(-1, 1),
60 SAMPLE4( 1, 1),
61 },
62 .origin = SAMPLE4(0, 0)
63 },
64
65 [MALI_SAMPLE_PATTERN_ROTATED_4X_GRID] = {
66 .positions = {
67 SAMPLE8(-1, -3),
68 SAMPLE8( 3, -1),
69 SAMPLE8(-3, 1),
70 SAMPLE8( 1, 3),
71 },
72 .origin = SAMPLE8(0, 0)
73 },
74
75 [MALI_SAMPLE_PATTERN_D3D_8X_GRID] = {
76 .positions = {
77 SAMPLE16( 1, -3),
78 SAMPLE16(-1, 3),
79 SAMPLE16( 5, 1),
80 SAMPLE16(-3, -5),
81 SAMPLE16(-5, 5),
82 SAMPLE16(-7, -1),
83 SAMPLE16( 3, 7),
84 SAMPLE16( 7, -7),
85 },
86 .origin = SAMPLE16(0, 0)
87 },
88
89 [MALI_SAMPLE_PATTERN_D3D_16X_GRID] = {
90 .positions = {
91 SAMPLE16( 1, 1),
92 SAMPLE16(-1, -3),
93 SAMPLE16(-3, 2),
94 SAMPLE16( 4, -1),
95 SAMPLE16(-5, -2),
96 SAMPLE16( 2, 5),
97 SAMPLE16( 5, 3),
98 SAMPLE16( 3, -5),
99 SAMPLE16(-2, 6),
100 SAMPLE16( 0, -7),
101 SAMPLE16(-4, -6),
102 SAMPLE16(-6, 4),
103 SAMPLE16(-8, 0),
104 SAMPLE16( 7, -4),
105 SAMPLE16( 6, 7),
106 SAMPLE16(-7, -8),
107 },
108 .origin = SAMPLE16(0, 0)
109 },
110
111 [MALI_SAMPLE_PATTERN_ROTATED_2X_GRID] = {
112 .positions = {
113 SAMPLE4( 1, 1),
114 SAMPLE4(-1, -1),
115 },
116 .origin = SAMPLE4(0, 0)
117 }
118 };
119 /* clang-format on */
120
121 unsigned
122 pan_sample_positions_buffer_size(void)
123 {
124 return sizeof(sample_position_lut);
125 }
126
127 unsigned
128 pan_sample_positions_offset(enum mali_sample_pattern pattern)
129 {
130 assert(pattern < ARRAY_SIZE(sample_position_lut));
131 unsigned offset = (pattern * sizeof(sample_position_lut[0]));
132 return offset;
133 }
134
135 void
136 pan_upload_sample_positions(void *buffer)
137 {
138 memcpy(buffer, sample_position_lut, sizeof(sample_position_lut));
139 }