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