Merge remote branch 'origin/mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / softpipe / sp_state_fs.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 #include "sp_context.h"
29 #include "sp_state.h"
30 #include "sp_fs.h"
31
32 #include "pipe/p_defines.h"
33 #include "util/u_memory.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_vs.h"
36 #include "tgsi/tgsi_dump.h"
37 #include "tgsi/tgsi_scan.h"
38 #include "tgsi/tgsi_parse.h"
39
40
41 void *
42 softpipe_create_fs_state(struct pipe_context *pipe,
43 const struct pipe_shader_state *templ)
44 {
45 struct softpipe_context *softpipe = softpipe_context(pipe);
46 struct sp_fragment_shader *state;
47
48 /* debug */
49 if (softpipe->dump_fs)
50 tgsi_dump(templ->tokens, 0);
51
52 /* codegen */
53 state = softpipe_create_fs_sse( softpipe, templ );
54 if (!state) {
55 state = softpipe_create_fs_exec( softpipe, templ );
56 }
57
58 assert(state);
59
60 /* get/save the summary info for this shader */
61 tgsi_scan_shader(templ->tokens, &state->info);
62
63 return state;
64 }
65
66
67 void
68 softpipe_bind_fs_state(struct pipe_context *pipe, void *fs)
69 {
70 struct softpipe_context *softpipe = softpipe_context(pipe);
71
72 softpipe->fs = (struct sp_fragment_shader *) fs;
73
74 softpipe->dirty |= SP_NEW_FS;
75 }
76
77
78 void
79 softpipe_delete_fs_state(struct pipe_context *pipe, void *fs)
80 {
81 struct sp_fragment_shader *state = fs;
82
83 assert(fs != softpipe_context(pipe)->fs);
84
85 state->delete( state );
86 }
87
88
89 void *
90 softpipe_create_vs_state(struct pipe_context *pipe,
91 const struct pipe_shader_state *templ)
92 {
93 struct softpipe_context *softpipe = softpipe_context(pipe);
94 struct sp_vertex_shader *state;
95
96 state = CALLOC_STRUCT(sp_vertex_shader);
97 if (state == NULL )
98 goto fail;
99
100 /* copy shader tokens, the ones passed in will go away.
101 */
102 state->shader.tokens = tgsi_dup_tokens(templ->tokens);
103 if (state->shader.tokens == NULL)
104 goto fail;
105
106 state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
107 if (state->draw_data == NULL)
108 goto fail;
109
110 state->max_sampler = state->draw_data->info.file_max[TGSI_FILE_SAMPLER];
111
112 return state;
113
114 fail:
115 if (state) {
116 FREE( (void *)state->shader.tokens );
117 FREE( state->draw_data );
118 FREE( state );
119 }
120 return NULL;
121 }
122
123
124 void
125 softpipe_bind_vs_state(struct pipe_context *pipe, void *vs)
126 {
127 struct softpipe_context *softpipe = softpipe_context(pipe);
128
129 softpipe->vs = (struct sp_vertex_shader *) vs;
130
131 draw_bind_vertex_shader(softpipe->draw,
132 (softpipe->vs ? softpipe->vs->draw_data : NULL));
133
134 softpipe->dirty |= SP_NEW_VS;
135 }
136
137
138 void
139 softpipe_delete_vs_state(struct pipe_context *pipe, void *vs)
140 {
141 struct softpipe_context *softpipe = softpipe_context(pipe);
142
143 struct sp_vertex_shader *state = (struct sp_vertex_shader *) vs;
144
145 draw_delete_vertex_shader(softpipe->draw, state->draw_data);
146 FREE( state );
147 }
148
149
150
151 void
152 softpipe_set_constant_buffer(struct pipe_context *pipe,
153 uint shader, uint index,
154 const struct pipe_constant_buffer *buf)
155 {
156 struct softpipe_context *softpipe = softpipe_context(pipe);
157
158 assert(shader < PIPE_SHADER_TYPES);
159 assert(index == 0);
160
161 /* note: reference counting */
162 pipe_buffer_reference(&softpipe->constants[shader].buffer,
163 buf ? buf->buffer : NULL);
164
165 softpipe->dirty |= SP_NEW_CONSTANTS;
166 }