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 "util/u_math.h"
35 #include "util/u_memory.h"
37 #include "pipe/p_shader_tokens.h"
39 #include "draw_private.h"
40 #include "draw_context.h"
43 #include "translate/translate.h"
44 #include "translate/translate_cache.h"
46 #include "tgsi/tgsi_dump.h"
47 #include "tgsi/tgsi_exec.h"
52 draw_vs_set_constants(struct draw_context
*draw
,
54 const void *constants
,
57 if (((uintptr_t)constants
) & 0xf) {
58 if (size
> draw
->vs
.const_storage_size
[slot
]) {
59 if (draw
->vs
.aligned_constant_storage
[slot
]) {
60 align_free((void *)draw
->vs
.aligned_constant_storage
[slot
]);
62 draw
->vs
.aligned_constant_storage
[slot
] = align_malloc(size
, 16);
65 memcpy((void *)draw
->vs
.aligned_constant_storage
[slot
],
68 constants
= draw
->vs
.aligned_constant_storage
[slot
];
71 draw
->vs
.aligned_constants
[slot
] = constants
;
72 draw_vs_aos_machine_constants(draw
->vs
.aos_machine
, slot
, constants
);
76 void draw_vs_set_viewport( struct draw_context
*draw
,
77 const struct pipe_viewport_state
*viewport
)
79 draw_vs_aos_machine_viewport( draw
->vs
.aos_machine
, viewport
);
84 struct draw_vertex_shader
*
85 draw_create_vertex_shader(struct draw_context
*draw
,
86 const struct pipe_shader_state
*shader
)
88 struct draw_vertex_shader
*vs
;
91 tgsi_dump(shader
->tokens
, 0);
94 vs
= draw_create_vs_llvm( draw
, shader
);
96 vs
= draw_create_vs_sse( draw
, shader
);
98 vs
= draw_create_vs_ppc( draw
, shader
);
100 vs
= draw_create_vs_exec( draw
, shader
);
108 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
109 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
110 vs
->info
.output_semantic_index
[i
] == 0)
111 vs
->position_output
= i
;
112 else if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_EDGEFLAG
&&
113 vs
->info
.output_semantic_index
[i
] == 0)
114 vs
->edgeflag_output
= i
;
124 draw_bind_vertex_shader(struct draw_context
*draw
,
125 struct draw_vertex_shader
*dvs
)
127 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
131 draw
->vs
.vertex_shader
= dvs
;
132 draw
->vs
.num_vs_outputs
= dvs
->info
.num_outputs
;
133 draw
->vs
.position_output
= dvs
->position_output
;
134 draw
->vs
.edgeflag_output
= dvs
->edgeflag_output
;
135 dvs
->prepare( dvs
, draw
);
138 draw
->vs
.vertex_shader
= NULL
;
139 draw
->vs
.num_vs_outputs
= 0;
145 draw_delete_vertex_shader(struct draw_context
*draw
,
146 struct draw_vertex_shader
*dvs
)
150 for (i
= 0; i
< dvs
->nr_varients
; i
++)
151 dvs
->varient
[i
]->destroy( dvs
->varient
[i
] );
153 dvs
->nr_varients
= 0;
161 draw_vs_init( struct draw_context
*draw
)
163 draw
->dump_vs
= debug_get_bool_option("GALLIUM_DUMP_VS", FALSE
);
165 draw
->vs
.machine
= tgsi_exec_machine_create();
166 if (!draw
->vs
.machine
)
169 draw
->vs
.emit_cache
= translate_cache_create();
170 if (!draw
->vs
.emit_cache
)
173 draw
->vs
.fetch_cache
= translate_cache_create();
174 if (!draw
->vs
.fetch_cache
)
177 draw
->vs
.aos_machine
= draw_vs_aos_machine();
179 if (!draw
->vs
.aos_machine
)
187 draw_vs_destroy( struct draw_context
*draw
)
191 if (draw
->vs
.fetch_cache
)
192 translate_cache_destroy(draw
->vs
.fetch_cache
);
194 if (draw
->vs
.emit_cache
)
195 translate_cache_destroy(draw
->vs
.emit_cache
);
197 if (draw
->vs
.aos_machine
)
198 draw_vs_aos_machine_destroy(draw
->vs
.aos_machine
);
200 for (i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
201 if (draw
->vs
.aligned_constant_storage
[i
]) {
202 align_free((void *)draw
->vs
.aligned_constant_storage
[i
]);
206 tgsi_exec_machine_destroy(draw
->vs
.machine
);
210 struct draw_vs_varient
*
211 draw_vs_lookup_varient( struct draw_vertex_shader
*vs
,
212 const struct draw_vs_varient_key
*key
)
214 struct draw_vs_varient
*varient
;
217 /* Lookup existing varient:
219 for (i
= 0; i
< vs
->nr_varients
; i
++)
220 if (draw_vs_varient_key_compare(key
, &vs
->varient
[i
]->key
) == 0)
221 return vs
->varient
[i
];
223 /* Else have to create a new one:
225 varient
= vs
->create_varient( vs
, key
);
229 /* Add it to our list, could be smarter:
231 if (vs
->nr_varients
< Elements(vs
->varient
)) {
232 vs
->varient
[vs
->nr_varients
++] = varient
;
236 vs
->last_varient
%= Elements(vs
->varient
);
237 vs
->varient
[vs
->last_varient
]->destroy(vs
->varient
[vs
->last_varient
]);
238 vs
->varient
[vs
->last_varient
] = varient
;
248 draw_vs_get_fetch( struct draw_context
*draw
,
249 struct translate_key
*key
)
251 if (!draw
->vs
.fetch
||
252 translate_key_compare(&draw
->vs
.fetch
->key
, key
) != 0)
254 translate_key_sanitize(key
);
255 draw
->vs
.fetch
= translate_cache_find(draw
->vs
.fetch_cache
, key
);
258 return draw
->vs
.fetch
;
262 draw_vs_get_emit( struct draw_context
*draw
,
263 struct translate_key
*key
)
265 if (!draw
->vs
.emit
||
266 translate_key_compare(&draw
->vs
.emit
->key
, key
) != 0)
268 translate_key_sanitize(key
);
269 draw
->vs
.emit
= translate_cache_find(draw
->vs
.emit_cache
, key
);
272 return draw
->vs
.emit
;