softpipe: Implement sampler view swizzling.
[mesa.git] / src / gallium / state_trackers / python / st_device.c
1 /**************************************************************************
2 *
3 * Copyright 2008 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 #include "pipe/p_screen.h"
30 #include "pipe/p_context.h"
31 #include "pipe/p_shader_tokens.h"
32 #include "util/u_inlines.h"
33 #include "cso_cache/cso_context.h"
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "util/u_sampler.h"
37 #include "util/u_simple_shaders.h"
38 #include "trace/tr_screen.h"
39 #include "trace/tr_context.h"
40
41 #include "st_device.h"
42 #include "st_winsys.h"
43
44
45 static void
46 st_device_really_destroy(struct st_device *st_dev)
47 {
48 if(st_dev->screen) {
49 /* FIXME: Don't really destroy until we keep track of every single
50 * reference or we end up causing a segmentation fault every time
51 * python exits. */
52 #if 0
53 st_dev->screen->destroy(st_dev->screen);
54 #endif
55 }
56
57 FREE(st_dev);
58 }
59
60
61 static void
62 st_device_reference(struct st_device **ptr, struct st_device *st_dev)
63 {
64 struct st_device *old_dev = *ptr;
65
66 if (pipe_reference(&(*ptr)->reference, &st_dev->reference))
67 st_device_really_destroy(old_dev);
68 *ptr = st_dev;
69 }
70
71
72 void
73 st_device_destroy(struct st_device *st_dev)
74 {
75 st_device_reference(&st_dev, NULL);
76 }
77
78
79 static struct st_device *
80 st_device_create_from_st_winsys(const struct st_winsys *st_ws)
81 {
82 struct st_device *st_dev;
83
84 if(!st_ws->screen_create)
85 return NULL;
86
87 st_dev = CALLOC_STRUCT(st_device);
88 if(!st_dev)
89 return NULL;
90
91 pipe_reference_init(&st_dev->reference, 1);
92 st_dev->st_ws = st_ws;
93
94 st_dev->real_screen = st_ws->screen_create();
95 if(!st_dev->real_screen) {
96 st_device_destroy(st_dev);
97 return NULL;
98 }
99
100 st_dev->screen = trace_screen_create(st_dev->real_screen);
101 if(!st_dev->screen) {
102 st_device_destroy(st_dev);
103 return NULL;
104 }
105
106 return st_dev;
107 }
108
109
110 struct st_device *
111 st_device_create(boolean hardware) {
112 if(hardware)
113 return st_device_create_from_st_winsys(&st_hardpipe_winsys);
114 else
115 return st_device_create_from_st_winsys(&st_softpipe_winsys);
116 }
117
118
119 void
120 st_context_destroy(struct st_context *st_ctx)
121 {
122 unsigned i;
123
124 if(st_ctx) {
125 struct st_device *st_dev = st_ctx->st_dev;
126
127 if(st_ctx->cso) {
128 cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
129 cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
130
131 cso_destroy_context(st_ctx->cso);
132 }
133
134 if(st_ctx->pipe)
135 st_ctx->pipe->destroy(st_ctx->pipe);
136
137 for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
138 pipe_sampler_view_reference(&st_ctx->fragment_sampler_views[i], NULL);
139 for(i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
140 pipe_sampler_view_reference(&st_ctx->vertex_sampler_views[i], NULL);
141 pipe_texture_reference(&st_ctx->default_texture, NULL);
142
143 FREE(st_ctx);
144
145 st_device_reference(&st_dev, NULL);
146 }
147 }
148
149
150 struct st_context *
151 st_context_create(struct st_device *st_dev)
152 {
153 struct st_context *st_ctx;
154
155 st_ctx = CALLOC_STRUCT(st_context);
156 if(!st_ctx)
157 return NULL;
158
159 st_device_reference(&st_ctx->st_dev, st_dev);
160
161 st_ctx->pipe = st_dev->screen->context_create(st_dev->screen, NULL);
162 if(!st_ctx->pipe) {
163 st_context_destroy(st_ctx);
164 return NULL;
165 }
166
167 st_ctx->cso = cso_create_context(st_ctx->pipe);
168 if(!st_ctx->cso) {
169 st_context_destroy(st_ctx);
170 return NULL;
171 }
172
173 /* disabled blending/masking */
174 {
175 struct pipe_blend_state blend;
176 memset(&blend, 0, sizeof(blend));
177 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
178 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
179 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
180 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
181 blend.rt[0].colormask = PIPE_MASK_RGBA;
182 cso_set_blend(st_ctx->cso, &blend);
183 }
184
185 /* no-op depth/stencil/alpha */
186 {
187 struct pipe_depth_stencil_alpha_state depthstencil;
188 memset(&depthstencil, 0, sizeof(depthstencil));
189 cso_set_depth_stencil_alpha(st_ctx->cso, &depthstencil);
190 }
191
192 /* rasterizer */
193 {
194 struct pipe_rasterizer_state rasterizer;
195 memset(&rasterizer, 0, sizeof(rasterizer));
196 rasterizer.front_winding = PIPE_WINDING_CW;
197 rasterizer.cull_mode = PIPE_WINDING_NONE;
198 cso_set_rasterizer(st_ctx->cso, &rasterizer);
199 }
200
201 /* clip */
202 {
203 struct pipe_clip_state clip;
204 memset(&clip, 0, sizeof(clip));
205 st_ctx->pipe->set_clip_state(st_ctx->pipe, &clip);
206 }
207
208 /* identity viewport */
209 {
210 struct pipe_viewport_state viewport;
211 viewport.scale[0] = 1.0;
212 viewport.scale[1] = 1.0;
213 viewport.scale[2] = 1.0;
214 viewport.scale[3] = 1.0;
215 viewport.translate[0] = 0.0;
216 viewport.translate[1] = 0.0;
217 viewport.translate[2] = 0.0;
218 viewport.translate[3] = 0.0;
219 cso_set_viewport(st_ctx->cso, &viewport);
220 }
221
222 /* samplers */
223 {
224 struct pipe_sampler_state sampler;
225 unsigned i;
226 memset(&sampler, 0, sizeof(sampler));
227 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
228 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
229 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
230 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
231 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
232 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
233 sampler.normalized_coords = 1;
234 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
235 cso_single_sampler(st_ctx->cso, i, &sampler);
236 cso_single_sampler_done(st_ctx->cso);
237 }
238
239 /* default textures */
240 {
241 struct pipe_screen *screen = st_dev->screen;
242 struct pipe_texture templat;
243 struct pipe_transfer *transfer;
244 struct pipe_sampler_view view_templ;
245 struct pipe_sampler_view *view;
246 unsigned i;
247
248 memset( &templat, 0, sizeof( templat ) );
249 templat.target = PIPE_TEXTURE_2D;
250 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
251 templat.width0 = 1;
252 templat.height0 = 1;
253 templat.depth0 = 1;
254 templat.last_level = 0;
255
256 st_ctx->default_texture = screen->texture_create( screen, &templat );
257 if(st_ctx->default_texture) {
258 transfer = screen->get_tex_transfer(screen,
259 st_ctx->default_texture,
260 0, 0, 0,
261 PIPE_TRANSFER_WRITE,
262 0, 0,
263 st_ctx->default_texture->width0,
264 st_ctx->default_texture->height0);
265 if (transfer) {
266 uint32_t *map;
267 map = (uint32_t *) screen->transfer_map(screen, transfer);
268 if(map) {
269 *map = 0x00000000;
270 screen->transfer_unmap(screen, transfer);
271 }
272 screen->tex_transfer_destroy(transfer);
273 }
274 }
275
276 u_sampler_view_default_template(&view_templ,
277 st_ctx->default_texture,
278 st_ctx->default_texture->format);
279 view = st_ctx->pipe->create_sampler_view(st_ctx->pipe,
280 st_ctx->default_texture,
281 &view_templ);
282
283 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
284 pipe_sampler_view_reference(&st_ctx->fragment_sampler_views[i], view);
285 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++)
286 pipe_sampler_view_reference(&st_ctx->vertex_sampler_views[i], view);
287
288 st_ctx->pipe->set_fragment_sampler_views(st_ctx->pipe,
289 PIPE_MAX_SAMPLERS,
290 st_ctx->fragment_sampler_views);
291 st_ctx->pipe->set_vertex_sampler_views(st_ctx->pipe,
292 PIPE_MAX_VERTEX_SAMPLERS,
293 st_ctx->vertex_sampler_views);
294
295 pipe_sampler_view_reference(&view, NULL);
296 }
297
298 /* vertex shader */
299 {
300 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
301 TGSI_SEMANTIC_GENERIC };
302 const uint semantic_indexes[] = { 0, 0 };
303 st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe,
304 2,
305 semantic_names,
306 semantic_indexes);
307 cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
308 }
309
310 /* fragment shader */
311 {
312 st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe);
313 cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
314 }
315
316 return st_ctx;
317 }