1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "pipe/p_util.h"
35 #include "draw_private.h"
36 #include "draw_context.h"
37 #include "draw_vertex.h"
39 #include "pipe/tgsi/exec/tgsi_core.h"
41 static INLINE
unsigned
42 compute_clipmask(float cx
, float cy
, float cz
, float cw
)
46 if (-cx
+ cw
< 0) mask
|= CLIP_RIGHT_BIT
;
47 if ( cx
+ cw
< 0) mask
|= CLIP_LEFT_BIT
;
48 if (-cy
+ cw
< 0) mask
|= CLIP_TOP_BIT
;
49 if ( cy
+ cw
< 0) mask
|= CLIP_BOTTOM_BIT
;
50 if (-cz
+ cw
< 0) mask
|= CLIP_FAR_BIT
;
51 if ( cz
+ cw
< 0) mask
|= CLIP_NEAR_BIT
;
59 #if !defined(XSTDCALL)
61 #define XSTDCALL __stdcall
67 typedef void (XSTDCALL
*codegen_function
) (
68 const struct tgsi_exec_vector
*input
,
69 struct tgsi_exec_vector
*output
,
71 struct tgsi_exec_vector
*temporary
);
74 * Transform vertices with the current vertex program/shader
75 * Up to four vertices can be shaded at a time.
76 * \param vbuffer the input vertex data
77 * \param elts indexes of four input vertices
78 * \param count number of vertices to shade [1..4]
79 * \param vOut array of pointers to four output vertices
82 run_vertex_program(struct draw_context
*draw
,
83 unsigned elts
[4], unsigned count
,
84 struct vertex_header
*vOut
[])
86 struct tgsi_exec_machine machine
;
89 ALIGN16_DECL(struct tgsi_exec_vector
, inputs
, PIPE_ATTRIB_MAX
);
90 ALIGN16_DECL(struct tgsi_exec_vector
, outputs
, PIPE_ATTRIB_MAX
);
91 const float *scale
= draw
->viewport
.scale
;
92 const float *trans
= draw
->viewport
.translate
;
95 assert(draw
->vertex_shader
.output_semantic_name
[0]
96 == TGSI_SEMANTIC_POSITION
);
99 memset( &machine
, 0, sizeof( machine
) );
102 /* init machine state */
103 tgsi_exec_machine_init(&machine
,
104 draw
->vertex_shader
.tokens
,
108 /* Consts does not require 16 byte alignment. */
109 machine
.Consts
= (float (*)[4]) draw
->mapped_constants
;
111 machine
.Inputs
= ALIGN16_ASSIGN(inputs
);
112 machine
.Outputs
= ALIGN16_ASSIGN(outputs
);
114 draw_vertex_fetch( draw
, &machine
, elts
, count
);
117 if( draw
->vertex_shader
.executable
!= NULL
) {
118 codegen_function func
= (codegen_function
) draw
->vertex_shader
.executable
;
126 tgsi_exec_machine_run( &machine
);
130 /* store machine results */
131 for (j
= 0; j
< count
; j
++) {
135 /* Handle attr[0] (position) specially: */
136 x
= vOut
[j
]->clip
[0] = machine
.Outputs
[0].xyzw
[0].f
[j
];
137 y
= vOut
[j
]->clip
[1] = machine
.Outputs
[0].xyzw
[1].f
[j
];
138 z
= vOut
[j
]->clip
[2] = machine
.Outputs
[0].xyzw
[2].f
[j
];
139 w
= vOut
[j
]->clip
[3] = machine
.Outputs
[0].xyzw
[3].f
[j
];
141 vOut
[j
]->clipmask
= compute_clipmask(x
, y
, z
, w
) | draw
->user_clipmask
;
142 vOut
[j
]->edgeflag
= 1;
150 /* Viewport mapping */
151 vOut
[j
]->data
[0][0] = x
* scale
[0] + trans
[0];
152 vOut
[j
]->data
[0][1] = y
* scale
[1] + trans
[1];
153 vOut
[j
]->data
[0][2] = z
* scale
[2] + trans
[2];
154 vOut
[j
]->data
[0][3] = w
;
156 /* Remaining attributes are packed into sequential post-transform
157 * vertex attrib slots.
158 * Skip 0 since we just did it above.
159 * Subtract two because of the VERTEX_HEADER, CLIP_POS attribs.
161 for (slot
= 1; slot
< draw
->vertex_info
.num_attribs
- 2; slot
++) {
162 vOut
[j
]->data
[slot
][0] = machine
.Outputs
[slot
].xyzw
[0].f
[j
];
163 vOut
[j
]->data
[slot
][1] = machine
.Outputs
[slot
].xyzw
[1].f
[j
];
164 vOut
[j
]->data
[slot
][2] = machine
.Outputs
[slot
].xyzw
[2].f
[j
];
165 vOut
[j
]->data
[slot
][3] = machine
.Outputs
[slot
].xyzw
[3].f
[j
];
167 printf("output %d: %f %f %f %f\n", slot,
168 vOut[j]->data[slot][0],
169 vOut[j]->data[slot][1],
170 vOut[j]->data[slot][2],
171 vOut[j]->data[slot][3]);
174 } /* loop over vertices */
179 * Called by the draw module when the vertx cache needs to be flushed.
180 * This involves running the vertex shader.
182 void draw_vertex_shader_queue_flush( struct draw_context
*draw
)
186 // fprintf(stderr, " q(%d) ", draw->vs.queue_nr );
188 /* run vertex shader on vertex cache entries, four per invokation */
189 for (i
= 0; i
< draw
->vs
.queue_nr
; i
+= 4) {
190 struct vertex_header
*dests
[4];
194 for (j
= 0; j
< 4; j
++) {
195 elts
[j
] = draw
->vs
.queue
[i
+ j
].elt
;
196 dests
[j
] = draw
->vs
.queue
[i
+ j
].dest
;
199 n
= MIN2(4, draw
->vs
.queue_nr
- i
);
203 run_vertex_program(draw
, elts
, n
, dests
);
206 draw
->vs
.queue_nr
= 0;