xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_pipe_wide_point.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 /* Authors:  Keith Whitwell <[email protected]>
29  */
30 
31 /**
32  * Notes on wide points and sprite mode:
33  *
34  * In wide point/sprite mode we effectively need to convert each incoming
35  * vertex into four outgoing vertices specifying the corners of a quad.
36  * Since we don't (yet) have geometry shaders, we have to handle this here
37  * in the draw module.
38  *
39  * For sprites, it also means that this is where we have to handle texcoords
40  * for the vertices of the quad.  OpenGL's GL_COORD_REPLACE state specifies
41  * if/how enabled texcoords are automatically generated for sprites.  We pass
42  * that info through gallium in the pipe_rasterizer_state::sprite_coord_mode
43  * array.
44  *
45  * Additionally, GLSL's gl_PointCoord fragment attribute has to be handled
46  * here as well.  This is basically an additional texture/generic attribute
47  * that varies .x from 0 to 1 horizontally across the point and varies .y
48  * vertically from 0 to 1 down the sprite.
49  *
50  * With geometry shaders, the gallium frontends could create a GS to do
51  * most/all of this.
52  */
53 
54 
55 #include "pipe/p_screen.h"
56 #include "pipe/p_context.h"
57 #include "util/u_math.h"
58 #include "util/u_memory.h"
59 #include "pipe/p_defines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "draw_fs.h"
62 #include "draw_vs.h"
63 #include "draw_pipe.h"
64 
65 
66 struct widepoint_stage {
67    struct draw_stage stage;  /**< base class */
68 
69    float half_point_size;
70 
71    float xbias;
72    float ybias;
73 
74    /** for automatic texcoord generation/replacement */
75    unsigned num_texcoord_gen;
76    unsigned texcoord_gen_slot[PIPE_MAX_SHADER_OUTPUTS];
77 
78    /* TGSI_SEMANTIC to which sprite_coord_enable applies */
79    enum tgsi_semantic sprite_coord_semantic;
80 
81    int psize_slot;
82 };
83 
84 
85 
86 static inline struct widepoint_stage *
widepoint_stage(struct draw_stage * stage)87 widepoint_stage(struct draw_stage *stage)
88 {
89    return (struct widepoint_stage *)stage;
90 }
91 
92 
93 /**
94  * Set the vertex texcoords for sprite mode.
95  * Coords may be left untouched or set to a right-side-up or upside-down
96  * orientation.
97  */
98 static void
set_texcoords(const struct widepoint_stage * wide,struct vertex_header * v,const float tc[4])99 set_texcoords(const struct widepoint_stage *wide,
100               struct vertex_header *v, const float tc[4])
101 {
102    const struct draw_context *draw = wide->stage.draw;
103    const struct pipe_rasterizer_state *rast = draw->rasterizer;
104    const unsigned texcoord_mode = rast->sprite_coord_mode;
105 
106    for (unsigned i = 0; i < wide->num_texcoord_gen; i++) {
107       const unsigned slot = wide->texcoord_gen_slot[i];
108       v->data[slot][0] = tc[0];
109       if (texcoord_mode == PIPE_SPRITE_COORD_LOWER_LEFT)
110          v->data[slot][1] = 1.0f - tc[1];
111       else
112          v->data[slot][1] = tc[1];
113       v->data[slot][2] = tc[2];
114       v->data[slot][3] = tc[3];
115    }
116 }
117 
118 
119 /* If there are lots of sprite points (and why wouldn't there be?) it
120  * would probably be more sensible to change hardware setup to
121  * optimize this rather than doing the whole thing in software like
122  * this.
123  */
124 static void
widepoint_point(struct draw_stage * stage,struct prim_header * header)125 widepoint_point(struct draw_stage *stage,
126                 struct prim_header *header)
127 {
128    const struct widepoint_stage *wide = widepoint_stage(stage);
129    const unsigned pos = draw_current_shader_position_output(stage->draw);
130    const bool sprite = (bool) stage->draw->rasterizer->point_quad_rasterization;
131    float half_size;
132    float left_adj, right_adj, bot_adj, top_adj;
133 
134    struct prim_header tri;
135 
136    /* four dups of original vertex */
137    struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
138    struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
139    struct vertex_header *v2 = dup_vert(stage, header->v[0], 2);
140    struct vertex_header *v3 = dup_vert(stage, header->v[0], 3);
141 
142    float *pos0 = v0->data[pos];
143    float *pos1 = v1->data[pos];
144    float *pos2 = v2->data[pos];
145    float *pos3 = v3->data[pos];
146 
147    /* point size is either per-vertex or fixed size */
148    if (wide->psize_slot >= 0) {
149       half_size = header->v[0]->data[wide->psize_slot][0];
150       half_size *= 0.5f;
151    }
152    else {
153       half_size = wide->half_point_size;
154    }
155 
156    left_adj = -half_size + wide->xbias;
157    right_adj = half_size + wide->xbias;
158    bot_adj = half_size + wide->ybias;
159    top_adj = -half_size + wide->ybias;
160 
161    pos0[0] += left_adj;
162    pos0[1] += top_adj;
163 
164    pos1[0] += left_adj;
165    pos1[1] += bot_adj;
166 
167    pos2[0] += right_adj;
168    pos2[1] += top_adj;
169 
170    pos3[0] += right_adj;
171    pos3[1] += bot_adj;
172 
173    if (sprite) {
174       static const float tex00[4] = { 0, 0, 0, 1 };
175       static const float tex01[4] = { 0, 1, 0, 1 };
176       static const float tex11[4] = { 1, 1, 0, 1 };
177       static const float tex10[4] = { 1, 0, 0, 1 };
178       set_texcoords(wide, v0, tex00);
179       set_texcoords(wide, v1, tex01);
180       set_texcoords(wide, v2, tex10);
181       set_texcoords(wide, v3, tex11);
182    }
183 
184    tri.det = header->det;  /* only the sign matters */
185    tri.v[0] = v0;
186    tri.v[1] = v2;
187    tri.v[2] = v3;
188    stage->next->tri(stage->next, &tri);
189 
190    tri.v[0] = v0;
191    tri.v[1] = v3;
192    tri.v[2] = v1;
193    stage->next->tri(stage->next, &tri);
194 }
195 
196 
197 static void
widepoint_first_point(struct draw_stage * stage,struct prim_header * header)198 widepoint_first_point(struct draw_stage *stage,
199                       struct prim_header *header)
200 {
201    struct widepoint_stage *wide = widepoint_stage(stage);
202    struct draw_context *draw = stage->draw;
203    struct pipe_context *pipe = draw->pipe;
204    const struct pipe_rasterizer_state *rast = draw->rasterizer;
205    void *r;
206 
207    wide->half_point_size = 0.5f * rast->point_size;
208    wide->xbias = 0.0;
209    wide->ybias = 0.0;
210 
211    if (rast->half_pixel_center) {
212       wide->xbias = 0.125;
213       wide->ybias = -0.125;
214    }
215 
216    /* Disable triangle culling, stippling, unfilled mode etc. */
217    r = draw_get_rasterizer_no_cull(draw, rast);
218    draw->suspend_flushing = true;
219    pipe->bind_rasterizer_state(pipe, r);
220    draw->suspend_flushing = false;
221 
222    /* XXX we won't know the real size if it's computed by the vertex shader! */
223    if ((rast->point_size > draw->pipeline.wide_point_threshold) ||
224        (rast->point_quad_rasterization && draw->pipeline.point_sprite)) {
225       stage->point = widepoint_point;
226    }
227    else {
228       stage->point = draw_pipe_passthrough_point;
229    }
230 
231    draw_remove_extra_vertex_attribs(draw);
232 
233    if (rast->point_quad_rasterization) {
234       const struct draw_fragment_shader *fs = draw->fs.fragment_shader;
235 
236       assert(fs);
237 
238       wide->num_texcoord_gen = 0;
239 
240       /* Loop over fragment shader inputs looking for the PCOORD input or inputs
241        * for which bit 'k' in sprite_coord_enable is set.
242        */
243       for (unsigned i = 0; i < fs->info.num_inputs; i++) {
244          int slot;
245          const enum tgsi_semantic sn = fs->info.input_semantic_name[i];
246          const unsigned si = fs->info.input_semantic_index[i];
247 
248          if (sn == wide->sprite_coord_semantic) {
249             /* Note that sprite_coord_enable is a bitfield of 32 bits. */
250             if (si >= 32 || !(rast->sprite_coord_enable & (1 << si)))
251                continue;
252          } else if (sn != TGSI_SEMANTIC_PCOORD) {
253             continue;
254          }
255 
256          /* OK, this generic attribute needs to be replaced with a
257           * sprite coord (see above).
258           */
259          slot = draw_alloc_extra_vertex_attrib(draw, sn, si);
260 
261          /* add this slot to the texcoord-gen list */
262          wide->texcoord_gen_slot[wide->num_texcoord_gen++] = slot;
263       }
264    }
265 
266    wide->psize_slot = -1;
267    if (rast->point_size_per_vertex) {
268       /* find PSIZ vertex output */
269       wide->psize_slot = draw_find_shader_output(draw, TGSI_SEMANTIC_PSIZE, 0);
270    }
271 
272    stage->point(stage, header);
273 }
274 
275 
276 static void
widepoint_flush(struct draw_stage * stage,unsigned flags)277 widepoint_flush(struct draw_stage *stage, unsigned flags)
278 {
279    struct draw_context *draw = stage->draw;
280    struct pipe_context *pipe = draw->pipe;
281 
282    stage->point = widepoint_first_point;
283    stage->next->flush(stage->next, flags);
284 
285    draw_remove_extra_vertex_attribs(draw);
286 
287    /* restore original rasterizer state */
288    if (draw->rast_handle) {
289       draw->suspend_flushing = true;
290       pipe->bind_rasterizer_state(pipe, draw->rast_handle);
291       draw->suspend_flushing = false;
292    }
293 }
294 
295 
296 static void
widepoint_reset_stipple_counter(struct draw_stage * stage)297 widepoint_reset_stipple_counter(struct draw_stage *stage)
298 {
299    stage->next->reset_stipple_counter(stage->next);
300 }
301 
302 
303 static void
widepoint_destroy(struct draw_stage * stage)304 widepoint_destroy(struct draw_stage *stage)
305 {
306    draw_free_temp_verts(stage);
307    FREE(stage);
308 }
309 
310 
draw_wide_point_stage(struct draw_context * draw)311 struct draw_stage *draw_wide_point_stage(struct draw_context *draw)
312 {
313    struct widepoint_stage *wide = CALLOC_STRUCT(widepoint_stage);
314    if (!wide)
315       goto fail;
316 
317    wide->stage.draw = draw;
318    wide->stage.name = "wide-point";
319    wide->stage.next = NULL;
320    wide->stage.point = widepoint_first_point;
321    wide->stage.line = draw_pipe_passthrough_line;
322    wide->stage.tri = draw_pipe_passthrough_tri;
323    wide->stage.flush = widepoint_flush;
324    wide->stage.reset_stipple_counter = widepoint_reset_stipple_counter;
325    wide->stage.destroy = widepoint_destroy;
326 
327    if (!draw_alloc_temp_verts(&wide->stage, 4))
328       goto fail;
329 
330    wide->sprite_coord_semantic =
331       draw->pipe->screen->get_param(draw->pipe->screen, PIPE_CAP_TGSI_TEXCOORD)
332       ?
333       TGSI_SEMANTIC_TEXCOORD : TGSI_SEMANTIC_GENERIC;
334 
335    return &wide->stage;
336 
337  fail:
338    if (wide)
339       wide->stage.destroy(&wide->stage);
340 
341    return NULL;
342 }
343