1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <stdint.h>
25
26 #include "pan_samples.h"
27
28 /* Sample positions are specified partially in hardware, partially in software
29 * on Mali. On Midgard, sample positions are completely fixed but need to be
30 * software accessible to implement gl_SamplePosition. On Bifrost, sample
31 * positions are part fixed, part programmable, and again need to be accessible
32 * for gl_SamplePosition. The mali_sample_position data structure is
33 * hardware-defined on Bifrost as a packed 8:8 fixed point format. The
34 * mali_sample_positions array is part-hardware, part-software-defined: it must
35 * give the sample position when indexed by the sample index, as well as
36 * reading back the origin when indexed by 32.
37 *
38 * The upshot is all of this is known at compile-time, so we can just hardcode
39 * the LUT, upload it to GPU memory on device initialization, and forget about
40 * it.
41 */
42
43 struct mali_sample_position {
44 uint16_t x, y;
45 } __attribute__((packed));
46
47 struct mali_sample_positions {
48 struct mali_sample_position positions[32];
49 struct mali_sample_position origin;
50 struct mali_sample_position padding[64 - (32 + 1)];
51 } __attribute__((packed));
52
53 /* SAMPLE16 constructs a single sample in terms of 1/16's of the grid, centered
54 * at the origin. SAMPLE4/8 swap the units for legibility. */
55
56 #define SAMPLE16(x, y) \
57 { \
58 (((x) + 8) * (256 / 16)), (((y) + 8) * (256 / 16)) \
59 }
60
61 #define SAMPLE8(x, y) SAMPLE16((x)*2, (y)*2)
62 #define SAMPLE4(x, y) SAMPLE16((x)*4, (y)*4)
63
64 /* clang-format off */
65 const struct mali_sample_positions sample_position_lut[] = {
66 [MALI_SAMPLE_PATTERN_SINGLE_SAMPLED] = {
67 .positions = {
68 SAMPLE4(0, 0)
69 },
70 .origin = SAMPLE4(0, 0)
71 },
72
73 [MALI_SAMPLE_PATTERN_ORDERED_4X_GRID] = {
74 .positions = {
75 SAMPLE4(-1, -1),
76 SAMPLE4( 1, -1),
77 SAMPLE4(-1, 1),
78 SAMPLE4( 1, 1),
79 },
80 .origin = SAMPLE4(0, 0)
81 },
82
83 [MALI_SAMPLE_PATTERN_ROTATED_4X_GRID] = {
84 .positions = {
85 SAMPLE8(-1, -3),
86 SAMPLE8( 3, -1),
87 SAMPLE8(-3, 1),
88 SAMPLE8( 1, 3),
89 },
90 .origin = SAMPLE8(0, 0)
91 },
92
93 [MALI_SAMPLE_PATTERN_D3D_8X_GRID] = {
94 .positions = {
95 SAMPLE16( 1, -3),
96 SAMPLE16(-1, 3),
97 SAMPLE16( 5, 1),
98 SAMPLE16(-3, -5),
99 SAMPLE16(-5, 5),
100 SAMPLE16(-7, -1),
101 SAMPLE16( 3, 7),
102 SAMPLE16( 7, -7),
103 },
104 .origin = SAMPLE16(0, 0)
105 },
106
107 [MALI_SAMPLE_PATTERN_D3D_16X_GRID] = {
108 .positions = {
109 SAMPLE16( 1, 1),
110 SAMPLE16(-1, -3),
111 SAMPLE16(-3, 2),
112 SAMPLE16( 4, -1),
113 SAMPLE16(-5, -2),
114 SAMPLE16( 2, 5),
115 SAMPLE16( 5, 3),
116 SAMPLE16( 3, -5),
117 SAMPLE16(-2, 6),
118 SAMPLE16( 0, 7),
119 SAMPLE16(-4, -6),
120 SAMPLE16(-6, 4),
121 SAMPLE16(-8, 0),
122 SAMPLE16( 7, -4),
123 SAMPLE16( 6, 7),
124 SAMPLE16(-7, -8),
125 },
126 .origin = SAMPLE16(0, 0)
127 }
128 };
129 /* clang-format on */
130
panfrost_sample_positions_buffer_size(void)131 unsigned panfrost_sample_positions_buffer_size(void)
132 {
133 return sizeof(sample_position_lut);
134 }
135
136 unsigned
panfrost_sample_positions_offset(enum mali_sample_pattern pattern)137 panfrost_sample_positions_offset(enum mali_sample_pattern pattern)
138 {
139 assert(pattern < ARRAY_SIZE(sample_position_lut));
140 unsigned offset = (pattern * sizeof(sample_position_lut[0]));
141 return offset;
142 }
143
144 void
panfrost_upload_sample_positions(void * buffer)145 panfrost_upload_sample_positions(void *buffer)
146 {
147 memcpy(buffer, sample_position_lut, sizeof(sample_position_lut));
148 }
149