draw_pipe_wide_point.c revision eb979cef8535914f428d2462e78f713da558fc18
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <keith@tungstengraphics.com>
29 */
30
31#include "util/u_math.h"
32#include "util/u_memory.h"
33#include "pipe/p_defines.h"
34#include "pipe/p_shader_tokens.h"
35#include "draw_vs.h"
36#include "draw_pipe.h"
37
38
39struct widepoint_stage {
40   struct draw_stage stage;
41
42   float half_point_size;
43   float point_size_min;
44   float point_size_max;
45
46   float xbias;
47   float ybias;
48
49   uint texcoord_slot[PIPE_MAX_SHADER_OUTPUTS];
50   uint texcoord_mode[PIPE_MAX_SHADER_OUTPUTS];
51   uint num_texcoords;
52
53   int psize_slot;
54
55   int point_coord_fs_input;  /**< input for pointcoord (and fog) */
56};
57
58
59
60static INLINE struct widepoint_stage *
61widepoint_stage( struct draw_stage *stage )
62{
63   return (struct widepoint_stage *)stage;
64}
65
66
67
68
69/**
70 * Set the vertex texcoords for sprite mode.
71 * Coords may be left untouched or set to a right-side-up or upside-down
72 * orientation.
73 */
74static void set_texcoords(const struct widepoint_stage *wide,
75                          struct vertex_header *v, const float tc[4])
76{
77   uint i;
78   for (i = 0; i < wide->num_texcoords; i++) {
79      if (wide->texcoord_mode[i] != PIPE_SPRITE_COORD_NONE) {
80         uint j = wide->texcoord_slot[i];
81         v->data[j][0] = tc[0];
82         if (wide->texcoord_mode[i] == PIPE_SPRITE_COORD_LOWER_LEFT)
83            v->data[j][1] = 1.0f - tc[1];
84         else
85            v->data[j][1] = tc[1];
86         v->data[j][2] = tc[2];
87         v->data[j][3] = tc[3];
88      }
89   }
90
91   if (wide->point_coord_fs_input >= 0) {
92      /* put gl_PointCoord into extra vertex output's zw components */
93      uint k = wide->stage.draw->extra_vp_outputs.slot;
94      v->data[k][2] = tc[0];
95      v->data[k][3] = tc[1];
96   }
97}
98
99
100/* If there are lots of sprite points (and why wouldn't there be?) it
101 * would probably be more sensible to change hardware setup to
102 * optimize this rather than doing the whole thing in software like
103 * this.
104 */
105static void widepoint_point( struct draw_stage *stage,
106                             struct prim_header *header )
107{
108   const struct widepoint_stage *wide = widepoint_stage(stage);
109   const unsigned pos = stage->draw->vs.position_output;
110   const boolean sprite = (boolean) stage->draw->rasterizer->point_sprite;
111   float half_size;
112   float left_adj, right_adj, bot_adj, top_adj;
113
114   struct prim_header tri;
115
116   /* four dups of original vertex */
117   struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
118   struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
119   struct vertex_header *v2 = dup_vert(stage, header->v[0], 2);
120   struct vertex_header *v3 = dup_vert(stage, header->v[0], 3);
121
122   float *pos0 = v0->data[pos];
123   float *pos1 = v1->data[pos];
124   float *pos2 = v2->data[pos];
125   float *pos3 = v3->data[pos];
126
127   /* point size is either per-vertex or fixed size */
128   if (wide->psize_slot >= 0) {
129      half_size = header->v[0]->data[wide->psize_slot][0];
130
131      /* XXX: temporary -- do this in the vertex shader??
132       */
133      half_size = CLAMP(half_size,
134                        wide->point_size_min,
135                        wide->point_size_max);
136
137      half_size *= 0.5f;
138   }
139   else {
140      half_size = wide->half_point_size;
141   }
142
143   left_adj = -half_size + wide->xbias;
144   right_adj = half_size + wide->xbias;
145   bot_adj = half_size + wide->ybias;
146   top_adj = -half_size + wide->ybias;
147
148   pos0[0] += left_adj;
149   pos0[1] += top_adj;
150
151   pos1[0] += left_adj;
152   pos1[1] += bot_adj;
153
154   pos2[0] += right_adj;
155   pos2[1] += top_adj;
156
157   pos3[0] += right_adj;
158   pos3[1] += bot_adj;
159
160   if (sprite) {
161      static const float tex00[4] = { 0, 0, 0, 1 };
162      static const float tex01[4] = { 0, 1, 0, 1 };
163      static const float tex11[4] = { 1, 1, 0, 1 };
164      static const float tex10[4] = { 1, 0, 0, 1 };
165      set_texcoords( wide, v0, tex00 );
166      set_texcoords( wide, v1, tex01 );
167      set_texcoords( wide, v2, tex10 );
168      set_texcoords( wide, v3, tex11 );
169   }
170
171   tri.det = header->det;  /* only the sign matters */
172   tri.v[0] = v0;
173   tri.v[1] = v2;
174   tri.v[2] = v3;
175   stage->next->tri( stage->next, &tri );
176
177   tri.v[0] = v0;
178   tri.v[1] = v3;
179   tri.v[2] = v1;
180   stage->next->tri( stage->next, &tri );
181}
182
183
184static void widepoint_first_point( struct draw_stage *stage,
185			      struct prim_header *header )
186{
187   struct widepoint_stage *wide = widepoint_stage(stage);
188   struct draw_context *draw = stage->draw;
189
190   wide->half_point_size = 0.5f * draw->rasterizer->point_size;
191   wide->point_size_min = draw->rasterizer->point_size_min;
192   wide->point_size_max = draw->rasterizer->point_size_max;
193   wide->xbias = 0.0;
194   wide->ybias = 0.0;
195
196   if (draw->rasterizer->gl_rasterization_rules) {
197      wide->xbias = 0.125;
198   }
199
200   /* XXX we won't know the real size if it's computed by the vertex shader! */
201   if ((draw->rasterizer->point_size > draw->pipeline.wide_point_threshold) ||
202       (draw->rasterizer->point_sprite && draw->pipeline.point_sprite)) {
203      stage->point = widepoint_point;
204   }
205   else {
206      stage->point = draw_pipe_passthrough_point;
207   }
208
209   if (draw->rasterizer->point_sprite) {
210      /* find vertex shader texcoord outputs */
211      const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
212      uint i, j = 0;
213      for (i = 0; i < vs->info.num_outputs; i++) {
214         if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
215            wide->texcoord_slot[j] = i;
216            wide->texcoord_mode[j] = draw->rasterizer->sprite_coord_mode[j];
217            j++;
218         }
219      }
220      wide->num_texcoords = j;
221
222      /* find fragment shader PointCoord/Fog input */
223      wide->point_coord_fs_input = 0; /* XXX fix this! */
224
225      /* setup extra vp output (point coord implemented as a texcoord) */
226      draw->extra_vp_outputs.semantic_name = TGSI_SEMANTIC_GENERIC;
227      draw->extra_vp_outputs.semantic_index = 0;
228      draw->extra_vp_outputs.slot = draw->vs.num_vs_outputs;
229   }
230   else {
231      wide->point_coord_fs_input = -1;
232      draw->extra_vp_outputs.slot = 0;
233   }
234
235   wide->psize_slot = -1;
236   if (draw->rasterizer->point_size_per_vertex) {
237      /* find PSIZ vertex output */
238      const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
239      uint i;
240      for (i = 0; i < vs->info.num_outputs; i++) {
241         if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_PSIZE) {
242            wide->psize_slot = i;
243            break;
244         }
245      }
246   }
247
248   stage->point( stage, header );
249}
250
251
252static void widepoint_flush( struct draw_stage *stage, unsigned flags )
253{
254   stage->point = widepoint_first_point;
255   stage->next->flush( stage->next, flags );
256}
257
258
259static void widepoint_reset_stipple_counter( struct draw_stage *stage )
260{
261   stage->next->reset_stipple_counter( stage->next );
262}
263
264
265static void widepoint_destroy( struct draw_stage *stage )
266{
267   draw_free_temp_verts( stage );
268   FREE( stage );
269}
270
271
272struct draw_stage *draw_wide_point_stage( struct draw_context *draw )
273{
274   struct widepoint_stage *wide = CALLOC_STRUCT(widepoint_stage);
275   if (wide == NULL)
276      goto fail;
277
278   if (!draw_alloc_temp_verts( &wide->stage, 4 ))
279      goto fail;
280
281   wide->stage.draw = draw;
282   wide->stage.name = "wide-point";
283   wide->stage.next = NULL;
284   wide->stage.point = widepoint_first_point;
285   wide->stage.line = draw_pipe_passthrough_line;
286   wide->stage.tri = draw_pipe_passthrough_tri;
287   wide->stage.flush = widepoint_flush;
288   wide->stage.reset_stipple_counter = widepoint_reset_stipple_counter;
289   wide->stage.destroy = widepoint_destroy;
290
291   return &wide->stage;
292
293 fail:
294   if (wide)
295      wide->stage.destroy( &wide->stage );
296
297   return NULL;
298}
299