1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
28 #include "util/u_memory.h"
29 #include "draw/draw_context.h"
30 #include "draw/draw_private.h"
31 #include "draw/draw_vbuf.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_pt.h"
34 #include "translate/translate.h"
35 #include "translate/translate_cache.h"
39 struct draw_context
*draw
;
41 struct translate
*translate
;
44 boolean need_edgeflags
;
46 struct translate_cache
*cache
;
49 /* Perform the fetch from API vertex elements & vertex buffers, to a
50 * contiguous set of float[4] attributes as required for the
51 * vertex_shader->run_linear() method.
53 * This is used in all cases except pure passthrough
54 * (draw_pt_fetch_emit.c) which has its own version to translate
55 * directly to hw vertices.
58 void draw_pt_fetch_prepare( struct pt_fetch
*fetch
,
59 unsigned vertex_size
)
61 struct draw_context
*draw
= fetch
->draw
;
63 unsigned dst_offset
= 0;
64 struct translate_key key
;
66 fetch
->vertex_size
= vertex_size
;
68 /* Always emit/leave space for a vertex header.
70 * It's worth considering whether the vertex headers should contain
71 * a pointer to the 'data', rather than having it inline.
72 * Something to look at after we've fully switched over to the pt
76 /* Need to set header->vertex_id = 0xffff somehow.
78 key
.element
[nr
].input_format
= PIPE_FORMAT_R32_FLOAT
;
79 key
.element
[nr
].input_buffer
= draw
->pt
.nr_vertex_buffers
;
80 key
.element
[nr
].input_offset
= 0;
81 key
.element
[nr
].output_format
= PIPE_FORMAT_R32_FLOAT
;
82 key
.element
[nr
].output_offset
= dst_offset
;
83 dst_offset
+= 1 * sizeof(float);
87 /* Just leave the clip[] array untouched.
89 dst_offset
+= 4 * sizeof(float);
93 for (i
= 0; i
< draw
->pt
.nr_vertex_elements
; i
++) {
94 key
.element
[nr
].input_format
= draw
->pt
.vertex_element
[i
].src_format
;
95 key
.element
[nr
].input_buffer
= draw
->pt
.vertex_element
[i
].vertex_buffer_index
;
96 key
.element
[nr
].input_offset
= draw
->pt
.vertex_element
[i
].src_offset
;
97 key
.element
[nr
].output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
98 key
.element
[nr
].output_offset
= dst_offset
;
100 dst_offset
+= 4 * sizeof(float);
104 assert(dst_offset
<= vertex_size
);
106 key
.nr_elements
= nr
;
107 key
.output_stride
= vertex_size
;
110 if (!fetch
->translate
||
111 translate_key_compare(&fetch
->translate
->key
, &key
) != 0)
113 translate_key_sanitize(&key
);
114 fetch
->translate
= translate_cache_find(fetch
->cache
, &key
);
117 static struct vertex_header vh
= { 0, 1, 0, 0xffff };
118 fetch
->translate
->set_buffer(fetch
->translate
,
119 draw
->pt
.nr_vertex_buffers
,
125 fetch
->need_edgeflags
= ((draw
->rasterizer
->fill_cw
!= PIPE_POLYGON_MODE_FILL
||
126 draw
->rasterizer
->fill_ccw
!= PIPE_POLYGON_MODE_FILL
) &&
127 draw
->pt
.user
.edgeflag
);
133 void draw_pt_fetch_run( struct pt_fetch
*fetch
,
134 const unsigned *elts
,
138 struct draw_context
*draw
= fetch
->draw
;
139 struct translate
*translate
= fetch
->translate
;
142 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
143 translate
->set_buffer(translate
,
145 ((char *)draw
->pt
.user
.vbuffer
[i
] +
146 draw
->pt
.vertex_buffer
[i
].buffer_offset
),
147 draw
->pt
.vertex_buffer
[i
].stride
);
150 translate
->run_elts( translate
,
155 /* Edgeflags are hard to fit into a translate program, populate
156 * them separately if required. In the setup above they are
157 * defaulted to one, so only need this if there is reason to change
160 if (fetch
->need_edgeflags
) {
161 for (i
= 0; i
< count
; i
++) {
162 struct vertex_header
*vh
= (struct vertex_header
*)(verts
+ i
* fetch
->vertex_size
);
163 vh
->edgeflag
= draw_pt_get_edgeflag( draw
, elts
[i
] );
169 void draw_pt_fetch_run_linear( struct pt_fetch
*fetch
,
174 struct draw_context
*draw
= fetch
->draw
;
175 struct translate
*translate
= fetch
->translate
;
178 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
179 translate
->set_buffer(translate
,
181 ((char *)draw
->pt
.user
.vbuffer
[i
] +
182 draw
->pt
.vertex_buffer
[i
].buffer_offset
),
183 draw
->pt
.vertex_buffer
[i
].stride
);
186 translate
->run( translate
,
191 /* Edgeflags are hard to fit into a translate program, populate
192 * them separately if required. In the setup above they are
193 * defaulted to one, so only need this if there is reason to change
196 if (fetch
->need_edgeflags
) {
197 for (i
= 0; i
< count
; i
++) {
198 struct vertex_header
*vh
= (struct vertex_header
*)(verts
+ i
* fetch
->vertex_size
);
199 vh
->edgeflag
= draw_pt_get_edgeflag( draw
, start
+ i
);
205 struct pt_fetch
*draw_pt_fetch_create( struct draw_context
*draw
)
207 struct pt_fetch
*fetch
= CALLOC_STRUCT(pt_fetch
);
212 fetch
->cache
= translate_cache_create();
221 void draw_pt_fetch_destroy( struct pt_fetch
*fetch
)
224 translate_cache_destroy(fetch
->cache
);