xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/svga/svga_pipe_misc.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "util/u_framebuffer.h"
9 #include "util/u_inlines.h"
10 #include "util/u_pstipple.h"
11 
12 #include "svga_cmd.h"
13 #include "svga_context.h"
14 #include "svga_screen.h"
15 #include "svga_surface.h"
16 #include "svga_resource_texture.h"
17 
18 
19 static void
svga_set_scissor_states(struct pipe_context * pipe,unsigned start_slot,unsigned num_scissors,const struct pipe_scissor_state * scissors)20 svga_set_scissor_states(struct pipe_context *pipe,
21                         unsigned start_slot,
22                         unsigned num_scissors,
23                         const struct pipe_scissor_state *scissors)
24 {
25    ASSERTED struct svga_screen *svgascreen = svga_screen(pipe->screen);
26    struct svga_context *svga = svga_context(pipe);
27    unsigned i, num_sc;
28 
29    assert(start_slot + num_scissors <= svgascreen->max_viewports);
30 
31    for (i = 0, num_sc = start_slot; i < num_scissors; i++)  {
32       svga->curr.scissor[num_sc++] = scissors[i]; /* struct copy */
33    }
34 
35    svga->dirty |= SVGA_NEW_SCISSOR;
36 }
37 
38 
39 static void
svga_set_polygon_stipple(struct pipe_context * pipe,const struct pipe_poly_stipple * stipple)40 svga_set_polygon_stipple(struct pipe_context *pipe,
41                          const struct pipe_poly_stipple *stipple)
42 {
43    struct svga_context *svga = svga_context(pipe);
44 
45    /* release old texture */
46    pipe_resource_reference(&svga->polygon_stipple.texture, NULL);
47 
48    /* release old sampler view */
49    if (svga->polygon_stipple.sampler_view) {
50       pipe->sampler_view_destroy(pipe,
51                                  &svga->polygon_stipple.sampler_view->base);
52    }
53 
54    /* create new stipple texture */
55    svga->polygon_stipple.texture =
56       util_pstipple_create_stipple_texture(pipe, stipple->stipple);
57 
58    /* create new sampler view */
59    svga->polygon_stipple.sampler_view =
60       (struct svga_pipe_sampler_view *)
61       util_pstipple_create_sampler_view(pipe,
62                                         svga->polygon_stipple.texture);
63 
64    /* allocate sampler state, if first time */
65    if (!svga->polygon_stipple.sampler) {
66       svga->polygon_stipple.sampler = util_pstipple_create_sampler(pipe);
67    }
68 
69    svga->dirty |= SVGA_NEW_STIPPLE;
70 }
71 
72 
73 void
svga_cleanup_framebuffer(struct svga_context * svga)74 svga_cleanup_framebuffer(struct svga_context *svga)
75 {
76    struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;
77    struct pipe_framebuffer_state *hw = &svga->state.hw_clear.framebuffer;
78 
79    util_unreference_framebuffer_state(curr);
80    util_unreference_framebuffer_state(hw);
81 }
82 
83 
84 #define DEPTH_BIAS_SCALE_FACTOR_D16    ((float)(1<<15))
85 #define DEPTH_BIAS_SCALE_FACTOR_D24S8  ((float)(1<<23))
86 #define DEPTH_BIAS_SCALE_FACTOR_D32    ((float)(1<<31))
87 
88 
89 static void
svga_set_framebuffer_state(struct pipe_context * pipe,const struct pipe_framebuffer_state * fb)90 svga_set_framebuffer_state(struct pipe_context *pipe,
91                            const struct pipe_framebuffer_state *fb)
92 {
93    struct svga_context *svga = svga_context(pipe);
94    struct pipe_framebuffer_state *dst = &svga->curr.framebuffer;
95    unsigned i;
96 
97    /* make sure any pending drawing calls are flushed before changing
98     * the framebuffer state
99     */
100    svga_hwtnl_flush_retry(svga);
101 
102    dst->width = fb->width;
103    dst->height = fb->height;
104    dst->nr_cbufs = fb->nr_cbufs;
105 
106    /* Check that all surfaces are the same size.
107     * Actually, the virtual hardware may support rendertargets with
108     * different size, depending on the host API and driver,
109     */
110    {
111       int width = 0, height = 0;
112       if (fb->zsbuf) {
113          width = fb->zsbuf->width;
114          height = fb->zsbuf->height;
115       }
116       for (i = 0; i < fb->nr_cbufs; ++i) {
117          if (fb->cbufs[i]) {
118             if (width && height) {
119                if (fb->cbufs[i]->width != width ||
120                    fb->cbufs[i]->height != height) {
121                   debug_warning("Mixed-size color and depth/stencil surfaces "
122                                 "may not work properly");
123                }
124             }
125             else {
126                width = fb->cbufs[i]->width;
127                height = fb->cbufs[i]->height;
128             }
129          }
130       }
131    }
132 
133    util_copy_framebuffer_state(dst, fb);
134 
135    if (svga->curr.framebuffer.zsbuf) {
136       switch (svga->curr.framebuffer.zsbuf->format) {
137       case PIPE_FORMAT_Z16_UNORM:
138          svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D16;
139          break;
140       case PIPE_FORMAT_Z24_UNORM_S8_UINT:
141       case PIPE_FORMAT_Z24X8_UNORM:
142       case PIPE_FORMAT_S8_UINT_Z24_UNORM:
143       case PIPE_FORMAT_X8Z24_UNORM:
144          svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D24S8;
145          break;
146       case PIPE_FORMAT_Z32_UNORM:
147          svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D32;
148          break;
149       case PIPE_FORMAT_Z32_FLOAT:
150          svga->curr.depthscale = 1.0f / ((float)(1<<23));
151          break;
152       default:
153          svga->curr.depthscale = 0.0f;
154          break;
155       }
156    }
157    else {
158       svga->curr.depthscale = 0.0f;
159    }
160 
161    svga->dirty |= SVGA_NEW_FRAME_BUFFER;
162 }
163 
164 
165 static void
svga_set_clip_state(struct pipe_context * pipe,const struct pipe_clip_state * clip)166 svga_set_clip_state(struct pipe_context *pipe,
167                     const struct pipe_clip_state *clip)
168 {
169    struct svga_context *svga = svga_context(pipe);
170 
171    svga->curr.clip = *clip; /* struct copy */
172 
173    svga->dirty |= SVGA_NEW_CLIP;
174 }
175 
176 
177 static void
svga_set_viewport_states(struct pipe_context * pipe,unsigned start_slot,unsigned num_viewports,const struct pipe_viewport_state * viewports)178 svga_set_viewport_states(struct pipe_context *pipe,
179                          unsigned start_slot,
180                          unsigned num_viewports,
181                          const struct pipe_viewport_state *viewports)
182 {
183    struct svga_context *svga = svga_context(pipe);
184    ASSERTED struct svga_screen *svgascreen = svga_screen(pipe->screen);
185    unsigned i, num_vp;
186 
187    assert(start_slot + num_viewports <= svgascreen->max_viewports);
188 
189    for (i = 0, num_vp = start_slot; i < num_viewports; i++)  {
190       svga->curr.viewport[num_vp++] = viewports[i]; /* struct copy */
191    }
192 
193    svga->dirty |= SVGA_NEW_VIEWPORT;
194 }
195 
196 
197 /**
198  * Called by state tracker to specify a callback function the driver
199  * can use to report info back to the gallium frontend.
200  */
201 static void
svga_set_debug_callback(struct pipe_context * pipe,const struct util_debug_callback * cb)202 svga_set_debug_callback(struct pipe_context *pipe,
203                         const struct util_debug_callback *cb)
204 {
205    struct svga_context *svga = svga_context(pipe);
206 
207    if (cb) {
208       svga->debug.callback = *cb;
209       svga->swc->debug_callback = &svga->debug.callback;
210    } else {
211       memset(&svga->debug.callback, 0, sizeof(svga->debug.callback));
212       svga->swc->debug_callback = NULL;
213    }
214 }
215 
216 
217 void
svga_init_misc_functions(struct svga_context * svga)218 svga_init_misc_functions(struct svga_context *svga)
219 {
220    svga->pipe.set_scissor_states = svga_set_scissor_states;
221    svga->pipe.set_polygon_stipple = svga_set_polygon_stipple;
222    svga->pipe.set_framebuffer_state = svga_set_framebuffer_state;
223    svga->pipe.set_clip_state = svga_set_clip_state;
224    svga->pipe.set_viewport_states = svga_set_viewport_states;
225    svga->pipe.set_debug_callback = svga_set_debug_callback;
226 }
227