Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[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_public.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 struct st_device *
79 st_device_create(boolean hardware)
80 {
81 struct pipe_screen *screen;
82 struct st_device *st_dev;
83
84 if (hardware)
85 screen = st_hardware_screen_create();
86 else
87 screen = st_software_screen_create("softpipe");
88
89 screen = trace_screen_create(screen);
90 if (!screen)
91 goto no_screen;
92
93 st_dev = CALLOC_STRUCT(st_device);
94 if (!st_dev)
95 goto no_device;
96
97 pipe_reference_init(&st_dev->reference, 1);
98 st_dev->screen = screen;
99
100 return st_dev;
101
102 no_device:
103 screen->destroy(screen);
104 no_screen:
105 return NULL;
106 }
107
108
109 void
110 st_context_destroy(struct st_context *st_ctx)
111 {
112 unsigned i;
113
114 if(st_ctx) {
115 struct st_device *st_dev = st_ctx->st_dev;
116
117 if(st_ctx->cso) {
118 cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
119 cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
120
121 cso_destroy_context(st_ctx->cso);
122 }
123
124 if(st_ctx->pipe)
125 st_ctx->pipe->destroy(st_ctx->pipe);
126
127 for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
128 pipe_sampler_view_reference(&st_ctx->fragment_sampler_views[i], NULL);
129 for(i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
130 pipe_sampler_view_reference(&st_ctx->vertex_sampler_views[i], NULL);
131 pipe_resource_reference(&st_ctx->default_texture, NULL);
132
133 FREE(st_ctx);
134
135 st_device_reference(&st_dev, NULL);
136 }
137 }
138
139
140 struct st_context *
141 st_context_create(struct st_device *st_dev)
142 {
143 struct st_context *st_ctx;
144
145 st_ctx = CALLOC_STRUCT(st_context);
146 if(!st_ctx)
147 return NULL;
148
149 st_device_reference(&st_ctx->st_dev, st_dev);
150
151 st_ctx->pipe = st_dev->screen->context_create(st_dev->screen, NULL);
152 if(!st_ctx->pipe) {
153 st_context_destroy(st_ctx);
154 return NULL;
155 }
156
157 st_ctx->cso = cso_create_context(st_ctx->pipe);
158 if(!st_ctx->cso) {
159 st_context_destroy(st_ctx);
160 return NULL;
161 }
162
163 /* disabled blending/masking */
164 {
165 struct pipe_blend_state blend;
166 memset(&blend, 0, sizeof(blend));
167 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
168 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
169 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
170 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
171 blend.rt[0].colormask = PIPE_MASK_RGBA;
172 cso_set_blend(st_ctx->cso, &blend);
173 }
174
175 /* no-op depth/stencil/alpha */
176 {
177 struct pipe_depth_stencil_alpha_state depthstencil;
178 memset(&depthstencil, 0, sizeof(depthstencil));
179 cso_set_depth_stencil_alpha(st_ctx->cso, &depthstencil);
180 }
181
182 /* rasterizer */
183 {
184 struct pipe_rasterizer_state rasterizer;
185 memset(&rasterizer, 0, sizeof(rasterizer));
186 rasterizer.cull_face = PIPE_FACE_NONE;
187 cso_set_rasterizer(st_ctx->cso, &rasterizer);
188 }
189
190 /* clip */
191 {
192 struct pipe_clip_state clip;
193 memset(&clip, 0, sizeof(clip));
194 st_ctx->pipe->set_clip_state(st_ctx->pipe, &clip);
195 }
196
197 /* identity viewport */
198 {
199 struct pipe_viewport_state viewport;
200 viewport.scale[0] = 1.0;
201 viewport.scale[1] = 1.0;
202 viewport.scale[2] = 1.0;
203 viewport.scale[3] = 1.0;
204 viewport.translate[0] = 0.0;
205 viewport.translate[1] = 0.0;
206 viewport.translate[2] = 0.0;
207 viewport.translate[3] = 0.0;
208 cso_set_viewport(st_ctx->cso, &viewport);
209 }
210
211 /* samplers */
212 {
213 struct pipe_sampler_state sampler;
214 unsigned i;
215 memset(&sampler, 0, sizeof(sampler));
216 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
217 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
218 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
219 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
220 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
221 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
222 sampler.normalized_coords = 1;
223 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
224 cso_single_sampler(st_ctx->cso, i, &sampler);
225 cso_single_sampler_done(st_ctx->cso);
226 }
227
228 /* default textures */
229 {
230 struct pipe_context *pipe = st_ctx->pipe;
231 struct pipe_screen *screen = st_dev->screen;
232 struct pipe_resource templat;
233 struct pipe_sampler_view view_templ;
234 struct pipe_sampler_view *view;
235 unsigned i;
236
237 memset( &templat, 0, sizeof( templat ) );
238 templat.target = PIPE_TEXTURE_2D;
239 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
240 templat.width0 = 1;
241 templat.height0 = 1;
242 templat.depth0 = 1;
243 templat.last_level = 0;
244 templat.bind = PIPE_BIND_SAMPLER_VIEW;
245
246 st_ctx->default_texture = screen->resource_create( screen, &templat );
247 if(st_ctx->default_texture) {
248 struct pipe_box box;
249 uint32_t zero = 0;
250
251 u_box_origin_2d( 1, 1, &box );
252
253 pipe->transfer_inline_write(pipe,
254 st_ctx->default_texture,
255 u_subresource(0,0),
256 PIPE_TRANSFER_WRITE,
257 &box,
258 &zero,
259 sizeof zero,
260 0);
261 }
262
263 u_sampler_view_default_template(&view_templ,
264 st_ctx->default_texture,
265 st_ctx->default_texture->format);
266 view = st_ctx->pipe->create_sampler_view(st_ctx->pipe,
267 st_ctx->default_texture,
268 &view_templ);
269
270 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
271 pipe_sampler_view_reference(&st_ctx->fragment_sampler_views[i], view);
272 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++)
273 pipe_sampler_view_reference(&st_ctx->vertex_sampler_views[i], view);
274
275 st_ctx->pipe->set_fragment_sampler_views(st_ctx->pipe,
276 PIPE_MAX_SAMPLERS,
277 st_ctx->fragment_sampler_views);
278 st_ctx->pipe->set_vertex_sampler_views(st_ctx->pipe,
279 PIPE_MAX_VERTEX_SAMPLERS,
280 st_ctx->vertex_sampler_views);
281
282 pipe_sampler_view_reference(&view, NULL);
283 }
284
285 /* vertex shader */
286 {
287 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
288 TGSI_SEMANTIC_GENERIC };
289 const uint semantic_indexes[] = { 0, 0 };
290 st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe,
291 2,
292 semantic_names,
293 semantic_indexes);
294 cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
295 }
296
297 /* fragment shader */
298 {
299 st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe);
300 cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
301 }
302
303 return st_ctx;
304 }