1 /*
2 * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "svga_cmd.h"
9 #include "svga_debug.h"
10
11 #include "pipe/p_defines.h"
12 #include "util/u_pack_color.h"
13 #include "util/u_surface.h"
14
15 #include "svga_context.h"
16 #include "svga_state.h"
17 #include "svga_surface.h"
18
19
20 /**
21 * Saving blitter states before doing any blitter operation
22 */
23 static void
begin_blit(struct svga_context * svga)24 begin_blit(struct svga_context *svga)
25 {
26 util_blitter_save_vertex_buffers(svga->blitter, svga->curr.vb,
27 svga->curr.num_vertex_buffers);
28 util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
29 util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
30 util_blitter_save_geometry_shader(svga->blitter, svga->curr.gs);
31 util_blitter_save_tessctrl_shader(svga->blitter, svga->curr.tcs);
32 util_blitter_save_tesseval_shader(svga->blitter, svga->curr.tes);
33 util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
34 (struct pipe_stream_output_target**)svga->so_targets);
35 util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
36 util_blitter_save_viewport(svga->blitter, &svga->curr.viewport[0]);
37 util_blitter_save_scissor(svga->blitter, &svga->curr.scissor[0]);
38 util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
39 util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
40 util_blitter_save_depth_stencil_alpha(svga->blitter,
41 (void*)svga->curr.depth);
42 util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
43 util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask, 0);
44 util_blitter_save_fragment_constant_buffer_slot(svga->blitter,
45 &svga->curr.constbufs[PIPE_SHADER_FRAGMENT][0]);
46 }
47
48
49 /**
50 * Clear the whole color buffer(s) by drawing a quad. For VGPU10 we use
51 * this when clearing integer render targets. We'll also clear the
52 * depth and/or stencil buffers if the clear_buffers mask specifies them.
53 */
54 static void
clear_buffers_with_quad(struct svga_context * svga,unsigned clear_buffers,const union pipe_color_union * color,double depth,unsigned stencil)55 clear_buffers_with_quad(struct svga_context *svga,
56 unsigned clear_buffers,
57 const union pipe_color_union *color,
58 double depth, unsigned stencil)
59 {
60 const struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;
61
62 begin_blit(svga);
63 util_blitter_clear(svga->blitter,
64 fb->width, fb->height,
65 1, /* num_layers */
66 clear_buffers, color,
67 depth, stencil,
68 util_framebuffer_get_num_samples(fb) > 1);
69 }
70
71
72 /**
73 * Check if any of the color buffers are integer buffers.
74 */
75 static bool
is_integer_target(struct pipe_framebuffer_state * fb,unsigned buffers)76 is_integer_target(struct pipe_framebuffer_state *fb, unsigned buffers)
77 {
78 unsigned i;
79
80 for (i = 0; i < fb->nr_cbufs; i++) {
81 if ((buffers & (PIPE_CLEAR_COLOR0 << i)) &&
82 fb->cbufs[i] &&
83 util_format_is_pure_integer(fb->cbufs[i]->format)) {
84 return true;
85 }
86 }
87 return false;
88 }
89
90
91 /**
92 * Check if the integer values in the clear color can be represented
93 * by floats. If so, we can use the VGPU10 ClearRenderTargetView command.
94 * Otherwise, we need to clear with a quad.
95 */
96 static bool
ints_fit_in_floats(const union pipe_color_union * color)97 ints_fit_in_floats(const union pipe_color_union *color)
98 {
99 const int max = 1 << 24;
100 return (color->i[0] <= max &&
101 color->i[1] <= max &&
102 color->i[2] <= max &&
103 color->i[3] <= max);
104 }
105
106
107 static enum pipe_error
try_clear(struct svga_context * svga,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)108 try_clear(struct svga_context *svga,
109 unsigned buffers,
110 const union pipe_color_union *color,
111 double depth,
112 unsigned stencil)
113 {
114 enum pipe_error ret = PIPE_OK;
115 SVGA3dRect rect = { 0, 0, 0, 0 };
116 bool restore_viewport = false;
117 SVGA3dClearFlag flags = 0;
118 struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;
119 union util_color uc = {0};
120
121 ret = svga_update_state(svga, SVGA_STATE_HW_CLEAR);
122 if (ret != PIPE_OK)
123 return ret;
124
125 if (svga->rebind.flags.rendertargets) {
126 ret = svga_reemit_framebuffer_bindings(svga);
127 if (ret != PIPE_OK) {
128 return ret;
129 }
130 }
131
132 if (buffers & PIPE_CLEAR_COLOR) {
133 flags |= SVGA3D_CLEAR_COLOR;
134 util_pack_color(color->f, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
135
136 rect.w = fb->width;
137 rect.h = fb->height;
138 }
139
140 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && fb->zsbuf) {
141 if (buffers & PIPE_CLEAR_DEPTH)
142 flags |= SVGA3D_CLEAR_DEPTH;
143
144 if (buffers & PIPE_CLEAR_STENCIL)
145 flags |= SVGA3D_CLEAR_STENCIL;
146
147 rect.w = MAX2(rect.w, fb->zsbuf->width);
148 rect.h = MAX2(rect.h, fb->zsbuf->height);
149 }
150
151 if (!svga_have_vgpu10(svga) &&
152 !svga_rects_equal(&rect, &svga->state.hw_clear.viewport)) {
153 restore_viewport = true;
154 ret = SVGA3D_SetViewport(svga->swc, &rect);
155 if (ret != PIPE_OK)
156 return ret;
157 }
158
159 if (svga_have_vgpu10(svga)) {
160 if (flags & SVGA3D_CLEAR_COLOR) {
161 unsigned i;
162 bool int_target = is_integer_target(fb, buffers);
163
164 if (int_target && !ints_fit_in_floats(color)) {
165 clear_buffers_with_quad(svga, buffers, color, depth, stencil);
166 /* We also cleared depth/stencil, so that's done */
167 flags &= ~(SVGA3D_CLEAR_DEPTH | SVGA3D_CLEAR_STENCIL);
168 }
169 else {
170 struct pipe_surface *rtv;
171 float rgba[4];
172
173 if (int_target) {
174 rgba[0] = (float) color->i[0];
175 rgba[1] = (float) color->i[1];
176 rgba[2] = (float) color->i[2];
177 rgba[3] = (float) color->i[3];
178 }
179 else {
180 rgba[0] = color->f[0];
181 rgba[1] = color->f[1];
182 rgba[2] = color->f[2];
183 rgba[3] = color->f[3];
184 }
185
186 /* Issue VGPU10 Clear commands */
187 for (i = 0; i < fb->nr_cbufs; i++) {
188 if ((fb->cbufs[i] == NULL) ||
189 !(buffers & (PIPE_CLEAR_COLOR0 << i)))
190 continue;
191
192 rtv = svga_validate_surface_view(svga,
193 svga_surface(fb->cbufs[i]));
194 if (!rtv)
195 return PIPE_ERROR_OUT_OF_MEMORY;
196
197 ret = SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv, rgba);
198 if (ret != PIPE_OK)
199 return ret;
200 }
201 }
202 }
203 if (flags & (SVGA3D_CLEAR_DEPTH | SVGA3D_CLEAR_STENCIL)) {
204 struct pipe_surface *dsv =
205 svga_validate_surface_view(svga, svga_surface(fb->zsbuf));
206 if (!dsv)
207 return PIPE_ERROR_OUT_OF_MEMORY;
208
209 ret = SVGA3D_vgpu10_ClearDepthStencilView(svga->swc, dsv, flags,
210 stencil, (float) depth);
211 if (ret != PIPE_OK)
212 return ret;
213 }
214 }
215 else {
216 ret = SVGA3D_ClearRect(svga->swc, flags, uc.ui[0], (float) depth, stencil,
217 rect.x, rect.y, rect.w, rect.h);
218 if (ret != PIPE_OK)
219 return ret;
220 }
221
222 if (restore_viewport) {
223 ret = SVGA3D_SetViewport(svga->swc, &svga->state.hw_clear.viewport);
224 }
225
226 return ret;
227 }
228
229 /**
230 * Clear the given surface to the specified value.
231 * No masking, no scissor (clear entire buffer).
232 */
233 static void
svga_clear(struct pipe_context * pipe,unsigned buffers,const struct pipe_scissor_state * scissor_state,const union pipe_color_union * color,double depth,unsigned stencil)234 svga_clear(struct pipe_context *pipe, unsigned buffers, const struct pipe_scissor_state *scissor_state,
235 const union pipe_color_union *color,
236 double depth, unsigned stencil)
237 {
238 struct svga_context *svga = svga_context( pipe );
239 enum pipe_error ret;
240
241 if (buffers & PIPE_CLEAR_COLOR) {
242 struct svga_winsys_surface *h = NULL;
243 if (svga->curr.framebuffer.cbufs[0]) {
244 h = svga_surface(svga->curr.framebuffer.cbufs[0])->handle;
245 }
246 SVGA_DBG(DEBUG_DMA, "clear sid %p\n", h);
247 }
248
249 /* flush any queued prims (don't want them to appear after the clear!) */
250 svga_hwtnl_flush_retry(svga);
251
252 SVGA_RETRY_OOM(svga, ret, try_clear( svga, buffers, color, depth, stencil));
253
254 /*
255 * Mark target surfaces as dirty
256 * TODO Mark only cleared surfaces.
257 */
258 svga_mark_surfaces_dirty(svga);
259
260 assert (ret == PIPE_OK);
261 }
262
263
264 static void
svga_clear_texture(struct pipe_context * pipe,struct pipe_resource * res,unsigned level,const struct pipe_box * box,const void * data)265 svga_clear_texture(struct pipe_context *pipe,
266 struct pipe_resource *res,
267 unsigned level,
268 const struct pipe_box *box,
269 const void *data)
270 {
271 struct svga_context *svga = svga_context(pipe);
272 struct svga_surface *svga_surface_dst;
273 struct pipe_surface tmpl;
274 struct pipe_surface *surface;
275
276 memset(&tmpl, 0, sizeof(tmpl));
277 tmpl.format = res->format;
278 tmpl.u.tex.first_layer = box->z;
279 tmpl.u.tex.last_layer = box->z + box->depth - 1;
280 tmpl.u.tex.level = level;
281
282 surface = pipe->create_surface(pipe, res, &tmpl);
283 if (surface == NULL) {
284 debug_printf("failed to create surface\n");
285 return;
286 }
287 svga_surface_dst = svga_surface(surface);
288
289 union pipe_color_union color;
290 const struct util_format_description *desc =
291 util_format_description(surface->format);
292
293 if (util_format_is_depth_or_stencil(surface->format)) {
294 float depth;
295 uint8_t stencil;
296 unsigned clear_flags = 0;
297
298 /* If data is NULL, then set depthValue and stencilValue to zeros */
299 if (data == NULL) {
300 depth = 0.0;
301 stencil = 0;
302 }
303 else {
304 util_format_unpack_z_float(surface->format, &depth, data, 1);
305 util_format_unpack_s_8uint(surface->format, &stencil, data, 1);
306 }
307
308 if (util_format_has_depth(desc)) {
309 clear_flags |= PIPE_CLEAR_DEPTH;
310 }
311 if (util_format_has_stencil(desc)) {
312 clear_flags |= PIPE_CLEAR_STENCIL;
313 }
314
315 /* Setup depth stencil view */
316 struct pipe_surface *dsv =
317 svga_validate_surface_view(svga, svga_surface_dst);
318
319 if (!dsv) {
320 pipe_surface_reference(&surface, NULL);
321 return;
322 }
323
324 if (box->x == 0 && box->y == 0 && box->width == surface->width &&
325 box->height == surface->height) {
326 /* clearing whole surface, use direct VGPU10 command */
327 assert(svga_surface(dsv)->view_id != SVGA3D_INVALID_ID);
328
329 SVGA_RETRY(svga, SVGA3D_vgpu10_ClearDepthStencilView(svga->swc, dsv,
330 clear_flags,
331 stencil, depth));
332 }
333 else {
334 /* To clear subtexture use software fallback */
335
336 util_blitter_save_framebuffer(svga->blitter,
337 &svga->curr.framebuffer);
338 begin_blit(svga);
339 util_blitter_clear_depth_stencil(svga->blitter,
340 dsv, clear_flags,
341 depth,stencil,
342 box->x, box->y,
343 box->width, box->height);
344 }
345 }
346 else {
347 /* non depth-stencil formats */
348
349 if (data == NULL) {
350 /* If data is NULL, the texture image is filled with zeros */
351 color.f[0] = color.f[1] = color.f[2] = color.f[3] = 0;
352 }
353 else {
354 util_format_unpack_rgba(surface->format, &color, data, 1);
355 }
356
357 /* Setup render target view */
358 struct pipe_surface *rtv =
359 svga_validate_surface_view(svga, svga_surface_dst);
360
361 if (!rtv) {
362 pipe_surface_reference(&surface, NULL);
363 return;
364 }
365
366 if (box->x == 0 && box->y == 0 && box->width == surface->width &&
367 box->height == surface->height) {
368 struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;
369 bool int_target = is_integer_target(curr, PIPE_CLEAR_COLOR);
370
371 if (int_target && !ints_fit_in_floats(&color)) {
372 /* To clear full texture with integer format */
373 clear_buffers_with_quad(svga, PIPE_CLEAR_COLOR, &color, 0.0, 0);
374 }
375 else {
376 float rgba[4];
377
378 if (int_target) {
379 rgba[0] = (float) color.i[0];
380 rgba[1] = (float) color.i[1];
381 rgba[2] = (float) color.i[2];
382 rgba[3] = (float) color.i[3];
383 }
384 else {
385 rgba[0] = color.f[0];
386 rgba[1] = color.f[1];
387 rgba[2] = color.f[2];
388 rgba[3] = color.f[3];
389 }
390
391 /* clearing whole surface using VGPU10 command */
392 assert(svga_surface(rtv)->view_id != SVGA3D_INVALID_ID);
393 SVGA_RETRY(svga, SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv,
394 rgba));
395 }
396 }
397 else {
398 /* To clear subtexture use software fallback */
399
400 /**
401 * util_blitter_clear_render_target doesn't support PIPE_TEXTURE_3D
402 * It tries to draw quad with depth 0 for PIPE_TEXTURE_3D so use
403 * util_clear_render_target() for PIPE_TEXTURE_3D.
404 */
405 if (rtv->texture->target != PIPE_TEXTURE_3D &&
406 pipe->screen->is_format_supported(pipe->screen, rtv->format,
407 rtv->texture->target,
408 rtv->texture->nr_samples,
409 rtv->texture->nr_storage_samples,
410 PIPE_BIND_RENDER_TARGET)) {
411 /* clear with quad drawing */
412 util_blitter_save_framebuffer(svga->blitter,
413 &svga->curr.framebuffer);
414 begin_blit(svga);
415 util_blitter_clear_render_target(svga->blitter,
416 rtv,
417 &color,
418 box->x, box->y,
419 box->width, box->height);
420 }
421 else {
422 /* clear with map/write/unmap */
423
424 /* store layer values */
425 unsigned first_layer = rtv->u.tex.first_layer;
426 unsigned last_layer = rtv->u.tex.last_layer;
427 unsigned box_depth = last_layer - first_layer + 1;
428
429 for (unsigned i = 0; i < box_depth; i++) {
430 rtv->u.tex.first_layer = rtv->u.tex.last_layer =
431 first_layer + i;
432 util_clear_render_target(pipe, rtv, &color, box->x, box->y,
433 box->width, box->height);
434 }
435 /* restore layer values */
436 rtv->u.tex.first_layer = first_layer;
437 rtv->u.tex.last_layer = last_layer;
438 }
439 }
440 }
441 pipe_surface_reference(&surface, NULL);
442 }
443
444 /**
445 * \brief Clear the whole render target using vgpu10 functionality
446 *
447 * \param svga[in] The svga context
448 * \param dst[in] The surface to clear
449 * \param color[in] Clear color
450 * \return PIPE_OK if all well, PIPE_ERROR_OUT_OF_MEMORY if ran out of
451 * command submission resources.
452 */
453 static enum pipe_error
svga_try_clear_render_target(struct svga_context * svga,struct pipe_surface * dst,const union pipe_color_union * color)454 svga_try_clear_render_target(struct svga_context *svga,
455 struct pipe_surface *dst,
456 const union pipe_color_union *color)
457 {
458 struct pipe_surface *rtv =
459 svga_validate_surface_view(svga, svga_surface(dst));
460
461 if (!rtv)
462 return PIPE_ERROR_OUT_OF_MEMORY;
463
464 assert(svga_surface(rtv)->view_id != SVGA3D_INVALID_ID);
465 return SVGA3D_vgpu10_ClearRenderTargetView(svga->swc, rtv, color->f);
466 }
467
468 /**
469 * \brief Clear part of render target using gallium blitter utilities
470 *
471 * \param svga[in] The svga context
472 * \param dst[in] The surface to clear
473 * \param color[in] Clear color
474 * \param dstx[in] Clear region left
475 * \param dsty[in] Clear region top
476 * \param width[in] Clear region width
477 * \param height[in] Clear region height
478 */
479 static void
svga_blitter_clear_render_target(struct svga_context * svga,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height)480 svga_blitter_clear_render_target(struct svga_context *svga,
481 struct pipe_surface *dst,
482 const union pipe_color_union *color,
483 unsigned dstx, unsigned dsty,
484 unsigned width, unsigned height)
485 {
486 begin_blit(svga);
487 util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
488
489 util_blitter_clear_render_target(svga->blitter, dst, color,
490 dstx, dsty, width, height);
491 }
492
493
494 /**
495 * \brief Clear render target pipe callback
496 *
497 * \param pipe[in] The pipe context
498 * \param dst[in] The surface to clear
499 * \param color[in] Clear color
500 * \param dstx[in] Clear region left
501 * \param dsty[in] Clear region top
502 * \param width[in] Clear region width
503 * \param height[in] Clear region height
504 * \param render_condition_enabled[in] Whether to use conditional rendering
505 * to clear (if elsewhere enabled).
506 */
507 static void
svga_clear_render_target(struct pipe_context * pipe,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)508 svga_clear_render_target(struct pipe_context *pipe,
509 struct pipe_surface *dst,
510 const union pipe_color_union *color,
511 unsigned dstx, unsigned dsty,
512 unsigned width, unsigned height,
513 bool render_condition_enabled)
514 {
515 struct svga_context *svga = svga_context( pipe );
516
517 svga_toggle_render_condition(svga, render_condition_enabled, false);
518 if (!svga_have_vgpu10(svga) || dstx != 0 || dsty != 0 ||
519 width != dst->width || height != dst->height) {
520 svga_blitter_clear_render_target(svga, dst, color, dstx, dsty, width,
521 height);
522 } else {
523 enum pipe_error ret;
524
525 SVGA_RETRY_OOM(svga, ret, svga_try_clear_render_target(svga, dst,
526 color));
527 assert (ret == PIPE_OK);
528 }
529 svga_toggle_render_condition(svga, render_condition_enabled, true);
530 }
531
svga_init_clear_functions(struct svga_context * svga)532 void svga_init_clear_functions(struct svga_context *svga)
533 {
534 svga->pipe.clear_render_target = svga_clear_render_target;
535 svga->pipe.clear_texture = svga_have_vgpu10(svga) ? svga_clear_texture : NULL;
536 svga->pipe.clear = svga_clear;
537 }
538