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