gallium: util_blit_pixels() takes source sampler view as argument.
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_cull.c
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 /**
29 * \brief Drawing stage for polygon culling
30 */
31
32 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
33 */
34
35
36 #include "util/u_memory.h"
37 #include "pipe/p_defines.h"
38 #include "draw_pipe.h"
39
40
41 struct cull_stage {
42 struct draw_stage stage;
43 unsigned winding; /**< which winding(s) to cull (one of PIPE_WINDING_x) */
44 };
45
46
47 static INLINE struct cull_stage *cull_stage( struct draw_stage *stage )
48 {
49 return (struct cull_stage *)stage;
50 }
51
52
53 static void cull_tri( struct draw_stage *stage,
54 struct prim_header *header )
55 {
56 const unsigned pos = draw_current_shader_position_output(stage->draw);
57
58 /* Window coords: */
59 const float *v0 = header->v[0]->data[pos];
60 const float *v1 = header->v[1]->data[pos];
61 const float *v2 = header->v[2]->data[pos];
62
63 /* edge vectors: e = v0 - v2, f = v1 - v2 */
64 const float ex = v0[0] - v2[0];
65 const float ey = v0[1] - v2[1];
66 const float fx = v1[0] - v2[0];
67 const float fy = v1[1] - v2[1];
68
69 /* det = cross(e,f).z */
70 header->det = ex * fy - ey * fx;
71
72 if (header->det != 0) {
73 /* if det < 0 then Z points toward the camera and the triangle is
74 * counter-clockwise winding.
75 */
76 unsigned winding = (header->det < 0) ? PIPE_WINDING_CCW : PIPE_WINDING_CW;
77
78 if ((winding & cull_stage(stage)->winding) == 0) {
79 /* triangle is not culled, pass to next stage */
80 stage->next->tri( stage->next, header );
81 }
82 }
83 }
84
85
86 static void cull_first_tri( struct draw_stage *stage,
87 struct prim_header *header )
88 {
89 struct cull_stage *cull = cull_stage(stage);
90
91 cull->winding = stage->draw->rasterizer->cull_mode;
92
93 stage->tri = cull_tri;
94 stage->tri( stage, header );
95 }
96
97
98 static void cull_flush( struct draw_stage *stage, unsigned flags )
99 {
100 stage->tri = cull_first_tri;
101 stage->next->flush( stage->next, flags );
102 }
103
104
105 static void cull_reset_stipple_counter( struct draw_stage *stage )
106 {
107 stage->next->reset_stipple_counter( stage->next );
108 }
109
110
111 static void cull_destroy( struct draw_stage *stage )
112 {
113 draw_free_temp_verts( stage );
114 FREE( stage );
115 }
116
117
118 /**
119 * Create a new polygon culling stage.
120 */
121 struct draw_stage *draw_cull_stage( struct draw_context *draw )
122 {
123 struct cull_stage *cull = CALLOC_STRUCT(cull_stage);
124 if (cull == NULL)
125 goto fail;
126
127 if (!draw_alloc_temp_verts( &cull->stage, 0 ))
128 goto fail;
129
130 cull->stage.draw = draw;
131 cull->stage.name = "cull";
132 cull->stage.next = NULL;
133 cull->stage.point = draw_pipe_passthrough_point;
134 cull->stage.line = draw_pipe_passthrough_line;
135 cull->stage.tri = cull_first_tri;
136 cull->stage.flush = cull_flush;
137 cull->stage.reset_stipple_counter = cull_reset_stipple_counter;
138 cull->stage.destroy = cull_destroy;
139
140 return &cull->stage;
141
142 fail:
143 if (cull)
144 cull->stage.destroy( &cull->stage );
145
146 return NULL;
147 }