2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Rob Clark <robclark@freedesktop.org>
28 #include "pipe/p_state.h"
30 #include "freedreno_resource.h"
31 #include "freedreno_state.h"
33 #include "fd6_image.h"
34 #include "fd6_format.h"
35 #include "fd6_resource.h"
36 #include "fd6_texture.h"
39 struct pipe_resource
*prsc
;
40 enum pipe_format pfmt
;
41 enum a6xx_tex_fmt fmt
;
42 enum a6xx_tex_fetchsize fetchsize
;
43 enum a6xx_tex_type type
;
58 static void translate_image(struct fd6_image
*img
, const struct pipe_image_view
*pimg
)
60 enum pipe_format format
= pimg
->format
;
61 struct pipe_resource
*prsc
= pimg
->resource
;
62 struct fd_resource
*rsc
= fd_resource(prsc
);
65 memset(img
, 0, sizeof(*img
));
71 img
->fmt
= fd6_pipe2tex(format
);
72 img
->fetchsize
= fd6_pipe2fetchsize(format
);
73 img
->type
= fd6_tex_type(prsc
->target
);
74 img
->srgb
= util_format_is_srgb(format
);
78 /* Treat cube textures as 2d-array: */
79 if (img
->type
== A6XX_TEX_CUBE
)
80 img
->type
= A6XX_TEX_2D
;
82 if (prsc
->target
== PIPE_BUFFER
) {
84 img
->ubwc_offset
= 0; /* not valid for buffers */
85 img
->offset
= pimg
->u
.buf
.offset
;
89 /* size is encoded with low 15b in WIDTH and high bits in
90 * HEIGHT, in units of elements:
92 unsigned sz
= prsc
->width0
;
93 img
->width
= sz
& MASK(15);
94 img
->height
= sz
>> 15;
99 unsigned lvl
= pimg
->u
.tex
.level
;
100 unsigned layers
= pimg
->u
.tex
.last_layer
- pimg
->u
.tex
.first_layer
+ 1;
102 img
->ubwc_offset
= fd_resource_ubwc_offset(rsc
, lvl
, pimg
->u
.tex
.first_layer
);
103 img
->offset
= fd_resource_offset(rsc
, lvl
, pimg
->u
.tex
.first_layer
);
104 img
->pitch
= rsc
->slices
[lvl
].pitch
* rsc
->cpp
;
106 switch (prsc
->target
) {
107 case PIPE_TEXTURE_RECT
:
108 case PIPE_TEXTURE_1D
:
109 case PIPE_TEXTURE_2D
:
110 img
->array_pitch
= rsc
->layer_size
;
113 case PIPE_TEXTURE_1D_ARRAY
:
114 case PIPE_TEXTURE_2D_ARRAY
:
115 case PIPE_TEXTURE_CUBE
:
116 case PIPE_TEXTURE_CUBE_ARRAY
:
117 img
->array_pitch
= rsc
->layer_size
;
118 // TODO the CUBE/CUBE_ARRAY might need to be layers/6 for tex state,
119 // but empirically for ibo state it shouldn't be divided.
122 case PIPE_TEXTURE_3D
:
123 img
->array_pitch
= rsc
->slices
[lvl
].size0
;
124 img
->depth
= u_minify(prsc
->depth0
, lvl
);
131 img
->width
= u_minify(prsc
->width0
, lvl
);
132 img
->height
= u_minify(prsc
->height0
, lvl
);
136 static void translate_buf(struct fd6_image
*img
, const struct pipe_shader_buffer
*pimg
)
138 enum pipe_format format
= PIPE_FORMAT_R32_UINT
;
139 struct pipe_resource
*prsc
= pimg
->buffer
;
140 struct fd_resource
*rsc
= fd_resource(prsc
);
143 memset(img
, 0, sizeof(*img
));
149 img
->fmt
= fd6_pipe2tex(format
);
150 img
->fetchsize
= fd6_pipe2fetchsize(format
);
151 img
->type
= fd6_tex_type(prsc
->target
);
152 img
->srgb
= util_format_is_srgb(format
);
157 img
->ubwc_offset
= 0; /* not valid for buffers */
158 img
->offset
= pimg
->buffer_offset
;
160 img
->array_pitch
= 0;
162 /* size is encoded with low 15b in WIDTH and high bits in HEIGHT,
163 * in units of elements:
165 unsigned sz
= pimg
->buffer_size
/ 4;
166 img
->width
= sz
& MASK(15);
167 img
->height
= sz
>> 15;
171 static void emit_image_tex(struct fd_ringbuffer
*ring
, struct fd6_image
*img
)
173 struct fd_resource
*rsc
= fd_resource(img
->prsc
);
174 bool ubwc_enabled
= fd_resource_ubwc_enabled(rsc
, img
->level
);
176 OUT_RING(ring
, fd6_tex_const_0(img
->prsc
, img
->level
, img
->pfmt
,
177 PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
,
178 PIPE_SWIZZLE_Z
, PIPE_SWIZZLE_W
));
179 OUT_RING(ring
, A6XX_TEX_CONST_1_WIDTH(img
->width
) |
180 A6XX_TEX_CONST_1_HEIGHT(img
->height
));
181 OUT_RING(ring
, A6XX_TEX_CONST_2_FETCHSIZE(img
->fetchsize
) |
182 COND(img
->buffer
, A6XX_TEX_CONST_2_UNK4
| A6XX_TEX_CONST_2_UNK31
) |
183 A6XX_TEX_CONST_2_TYPE(img
->type
) |
184 A6XX_TEX_CONST_2_PITCH(img
->pitch
));
185 OUT_RING(ring
, A6XX_TEX_CONST_3_ARRAY_PITCH(img
->array_pitch
) |
186 COND(ubwc_enabled
, A6XX_TEX_CONST_3_FLAG
| A6XX_TEX_CONST_3_TILE_ALL
));
188 OUT_RELOC(ring
, img
->bo
, img
->offset
,
189 (uint64_t)A6XX_TEX_CONST_5_DEPTH(img
->depth
) << 32, 0);
191 OUT_RING(ring
, 0x00000000);
192 OUT_RING(ring
, A6XX_TEX_CONST_5_DEPTH(img
->depth
));
195 OUT_RING(ring
, 0x00000000); /* texconst6 */
198 OUT_RELOC(ring
, rsc
->bo
, img
->ubwc_offset
, 0, 0);
199 OUT_RING(ring
, A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(rsc
->ubwc_size
));
200 OUT_RING(ring
, A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(rsc
->ubwc_pitch
));
202 OUT_RING(ring
, 0x00000000); /* texconst7 */
203 OUT_RING(ring
, 0x00000000); /* texconst8 */
204 OUT_RING(ring
, 0x00000000); /* texconst9 */
205 OUT_RING(ring
, 0x00000000); /* texconst10 */
208 OUT_RING(ring
, 0x00000000); /* texconst11 */
209 OUT_RING(ring
, 0x00000000); /* texconst12 */
210 OUT_RING(ring
, 0x00000000); /* texconst13 */
211 OUT_RING(ring
, 0x00000000); /* texconst14 */
212 OUT_RING(ring
, 0x00000000); /* texconst15 */
216 fd6_emit_image_tex(struct fd_ringbuffer
*ring
, const struct pipe_image_view
*pimg
)
218 struct fd6_image img
;
219 translate_image(&img
, pimg
);
220 emit_image_tex(ring
, &img
);
224 fd6_emit_ssbo_tex(struct fd_ringbuffer
*ring
, const struct pipe_shader_buffer
*pbuf
)
226 struct fd6_image img
;
227 translate_buf(&img
, pbuf
);
228 emit_image_tex(ring
, &img
);
231 static void emit_image_ssbo(struct fd_ringbuffer
*ring
, struct fd6_image
*img
)
233 struct fd_resource
*rsc
= fd_resource(img
->prsc
);
234 enum a6xx_tile_mode tile_mode
= TILE6_LINEAR
;
235 bool ubwc_enabled
= fd_resource_ubwc_enabled(rsc
, img
->level
);
237 if (rsc
->tile_mode
&& !fd_resource_level_linear(img
->prsc
, img
->level
)) {
238 tile_mode
= rsc
->tile_mode
;
241 OUT_RING(ring
, A6XX_IBO_0_FMT(img
->fmt
) |
242 A6XX_IBO_0_TILE_MODE(tile_mode
));
243 OUT_RING(ring
, A6XX_IBO_1_WIDTH(img
->width
) |
244 A6XX_IBO_1_HEIGHT(img
->height
));
245 OUT_RING(ring
, A6XX_IBO_2_PITCH(img
->pitch
) |
246 COND(img
->buffer
, A6XX_IBO_2_UNK4
| A6XX_IBO_2_UNK31
) |
247 A6XX_IBO_2_TYPE(img
->type
));
248 OUT_RING(ring
, A6XX_IBO_3_ARRAY_PITCH(img
->array_pitch
) |
249 COND(ubwc_enabled
, A6XX_IBO_3_FLAG
| A6XX_IBO_3_UNK27
));
251 OUT_RELOCW(ring
, img
->bo
, img
->offset
,
252 (uint64_t)A6XX_IBO_5_DEPTH(img
->depth
) << 32, 0);
254 OUT_RING(ring
, 0x00000000);
255 OUT_RING(ring
, A6XX_IBO_5_DEPTH(img
->depth
));
257 OUT_RING(ring
, 0x00000000);
260 OUT_RELOCW(ring
, rsc
->bo
, img
->ubwc_offset
, 0, 0);
261 OUT_RING(ring
, A6XX_IBO_9_FLAG_BUFFER_ARRAY_PITCH(rsc
->ubwc_size
));
262 OUT_RING(ring
, A6XX_IBO_10_FLAG_BUFFER_PITCH(rsc
->ubwc_pitch
));
264 OUT_RING(ring
, 0x00000000);
265 OUT_RING(ring
, 0x00000000);
266 OUT_RING(ring
, 0x00000000);
267 OUT_RING(ring
, 0x00000000);
270 OUT_RING(ring
, 0x00000000);
271 OUT_RING(ring
, 0x00000000);
272 OUT_RING(ring
, 0x00000000);
273 OUT_RING(ring
, 0x00000000);
274 OUT_RING(ring
, 0x00000000);
277 /* Build combined image/SSBO "IBO" state, returns ownership of state reference */
278 struct fd_ringbuffer
*
279 fd6_build_ibo_state(struct fd_context
*ctx
, const struct ir3_shader_variant
*v
,
280 enum pipe_shader_type shader
)
282 struct fd_shaderbuf_stateobj
*bufso
= &ctx
->shaderbuf
[shader
];
283 struct fd_shaderimg_stateobj
*imgso
= &ctx
->shaderimg
[shader
];
284 const struct ir3_ibo_mapping
*mapping
= &v
->image_mapping
;
286 struct fd_ringbuffer
*state
=
287 fd_submit_new_ringbuffer(ctx
->batch
->submit
,
288 mapping
->num_ibo
* 16 * 4, FD_RINGBUFFER_STREAMING
);
290 assert(shader
== PIPE_SHADER_COMPUTE
|| shader
== PIPE_SHADER_FRAGMENT
);
292 for (unsigned i
= 0; i
< mapping
->num_ibo
; i
++) {
293 struct fd6_image img
;
294 unsigned idx
= mapping
->ibo_to_image
[i
];
296 if (idx
& IBO_SSBO
) {
297 translate_buf(&img
, &bufso
->sb
[idx
& ~IBO_SSBO
]);
299 translate_image(&img
, &imgso
->si
[idx
]);
302 emit_image_ssbo(state
, &img
);
308 static void fd6_set_shader_images(struct pipe_context
*pctx
,
309 enum pipe_shader_type shader
,
310 unsigned start
, unsigned count
,
311 const struct pipe_image_view
*images
)
313 struct fd_context
*ctx
= fd_context(pctx
);
314 struct fd_shaderimg_stateobj
*so
= &ctx
->shaderimg
[shader
];
316 fd_set_shader_images(pctx
, shader
, start
, count
, images
);
321 for (unsigned i
= 0; i
< count
; i
++) {
322 unsigned n
= i
+ start
;
323 struct pipe_image_view
*buf
= &so
->si
[n
];
328 fd6_validate_format(ctx
, fd_resource(buf
->resource
), buf
->format
);
333 fd6_image_init(struct pipe_context
*pctx
)
335 pctx
->set_shader_images
= fd6_set_shader_images
;