1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #pragma once
7
8 #include <assert.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include "util/format/u_formats.h"
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /* Maximum render targets per framebuffer. This is NOT architectural, but it
18 * is the ~universal API limit so there's no point in allowing more.
19 */
20 #define AGX_MAX_RENDER_TARGETS (8)
21
22 /* Forward declarations to keep the header lean */
23 struct nir_shader;
24 struct nir_def;
25 struct nir_builder;
26 struct agx_usc_builder;
27
28 struct agx_tile_size {
29 uint8_t width;
30 uint8_t height;
31 };
32
33 struct agx_tilebuffer_layout {
34 /* Logical format of each render target. Use agx_tilebuffer_physical_format
35 * to get the physical format.
36 */
37 enum pipe_format logical_format[AGX_MAX_RENDER_TARGETS];
38
39 /* Which render targets are spilled. */
40 bool spilled[AGX_MAX_RENDER_TARGETS];
41
42 /* Offset into the sample of each render target. If a render target is
43 * spilled, its offset is UNDEFINED. Use agx_tilebuffer_offset_B to access.
44 */
45 uint8_t _offset_B[AGX_MAX_RENDER_TARGETS];
46
47 /* Total bytes per sample, rounded up as needed. Spilled render targets do
48 * not count against this.
49 */
50 uint8_t sample_size_B;
51
52 /* Number of samples per pixel */
53 uint8_t nr_samples;
54
55 /* If layered rendering is used */
56 bool layered;
57
58 /* Selected tile size */
59 struct agx_tile_size tile_size;
60 };
61
62 /*
63 * _offset_B is undefined for non-spilled render targets. This safe accessor
64 * asserts that render targets are not spilled rather than returning garbage.
65 */
66 static inline uint8_t
agx_tilebuffer_offset_B(struct agx_tilebuffer_layout * layout,unsigned rt)67 agx_tilebuffer_offset_B(struct agx_tilebuffer_layout *layout, unsigned rt)
68 {
69 assert(rt < AGX_MAX_RENDER_TARGETS);
70 assert(!layout->spilled[rt] && "precondition");
71
72 return layout->_offset_B[rt];
73 }
74
75 static inline bool
agx_tilebuffer_spills(struct agx_tilebuffer_layout * layout)76 agx_tilebuffer_spills(struct agx_tilebuffer_layout *layout)
77 {
78 for (unsigned rt = 0; rt < AGX_MAX_RENDER_TARGETS; ++rt) {
79 if (layout->spilled[rt])
80 return true;
81 }
82
83 return false;
84 }
85
86 struct agx_tilebuffer_layout
87 agx_build_tilebuffer_layout(enum pipe_format *formats, uint8_t nr_cbufs,
88 uint8_t nr_samples, bool layered);
89
90 bool agx_nir_lower_tilebuffer(struct nir_shader *shader,
91 struct agx_tilebuffer_layout *tib,
92 uint8_t *colormasks, unsigned *bindless_base,
93 bool *translucent, bool layer_id_sr);
94
95 struct nir_def *agx_internal_layer_id(struct nir_builder *b);
96
97 struct agx_msaa_state {
98 uint8_t nr_samples;
99
100 /* Enable API sample mask lowering (e.g. glSampleMask) */
101 bool api_sample_mask;
102 };
103
104 bool agx_nir_lower_monolithic_msaa(struct nir_shader *shader,
105 struct agx_msaa_state *state);
106
107 bool agx_nir_lower_sample_intrinsics(struct nir_shader *shader);
108
109 bool agx_nir_lower_alpha_to_coverage(struct nir_shader *shader,
110 uint8_t nr_samples);
111
112 bool agx_nir_lower_alpha_to_one(struct nir_shader *shader);
113
114 bool agx_nir_predicate_layer_id(struct nir_shader *shader);
115
116 void agx_usc_tilebuffer(struct agx_usc_builder *b,
117 struct agx_tilebuffer_layout *tib);
118
119 uint32_t agx_tilebuffer_total_size(struct agx_tilebuffer_layout *tib);
120
121 enum pipe_format
122 agx_tilebuffer_physical_format(struct agx_tilebuffer_layout *tib, unsigned rt);
123
124 bool agx_tilebuffer_supports_mask(struct agx_tilebuffer_layout *tib,
125 unsigned rt);
126
127 #ifdef __cplusplus
128 } /* extern C */
129 #endif
130