python: Allow to use trace pipe driver with python.
[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_util.h"
30 #include "pipe/p_winsys.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_shader_tokens.h"
33 #include "pipe/p_inlines.h"
34 #include "cso_cache/cso_context.h"
35 #include "util/u_simple_shaders.h"
36 #include "trace/tr_screen.h"
37 #include "trace/tr_context.h"
38
39 #include "st_device.h"
40 #include "st_winsys.h"
41
42
43 static void
44 st_device_really_destroy(struct st_device *st_dev)
45 {
46 if(st_dev->screen)
47 st_dev->screen->destroy(st_dev->screen);
48
49 FREE(st_dev);
50 }
51
52
53 void
54 st_device_destroy(struct st_device *st_dev)
55 {
56 if(!--st_dev->refcount)
57 st_device_really_destroy(st_dev);
58 }
59
60
61 static struct st_device *
62 st_device_create_from_st_winsys(const struct st_winsys *st_ws)
63 {
64 struct st_device *st_dev;
65
66 if(!st_ws->screen_create ||
67 !st_ws->context_create)
68 return NULL;
69
70 st_dev = CALLOC_STRUCT(st_device);
71 if(!st_dev)
72 return NULL;
73
74 st_dev->refcount = 1;
75 st_dev->st_ws = st_ws;
76
77 st_dev->real_screen = st_ws->screen_create();
78 if(!st_dev->real_screen) {
79 st_device_destroy(st_dev);
80 return NULL;
81 }
82
83 st_dev->screen = trace_screen_create(st_dev->real_screen);
84 if(!st_dev->screen) {
85 st_device_destroy(st_dev);
86 return NULL;
87 }
88
89 return st_dev;
90 }
91
92
93 struct st_device *
94 st_device_create(boolean hardware) {
95 if(hardware)
96 return st_device_create_from_st_winsys(&st_hardpipe_winsys);
97 else
98 return st_device_create_from_st_winsys(&st_softpipe_winsys);
99 }
100
101
102 void
103 st_context_destroy(struct st_context *st_ctx)
104 {
105 unsigned i;
106
107 if(st_ctx) {
108 struct st_device *st_dev = st_ctx->st_dev;
109
110 if(st_ctx->cso) {
111 cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
112 cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
113
114 cso_destroy_context(st_ctx->cso);
115 }
116
117 if(st_ctx->pipe)
118 st_ctx->pipe->destroy(st_ctx->pipe);
119
120 for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
121 pipe_texture_reference(&st_ctx->sampler_textures[i], NULL);
122 pipe_texture_reference(&st_ctx->default_texture, NULL);
123
124 FREE(st_ctx);
125
126 if(!--st_dev->refcount)
127 st_device_really_destroy(st_dev);
128 }
129 }
130
131
132 struct st_context *
133 st_context_create(struct st_device *st_dev)
134 {
135 struct st_context *st_ctx;
136
137 st_ctx = CALLOC_STRUCT(st_context);
138 if(!st_ctx)
139 return NULL;
140
141 st_ctx->st_dev = st_dev;
142 ++st_dev->refcount;
143
144 st_ctx->real_pipe = st_dev->st_ws->context_create(st_dev->real_screen);
145 if(!st_ctx->real_pipe) {
146 st_context_destroy(st_ctx);
147 return NULL;
148 }
149
150 st_ctx->pipe = trace_context_create(st_dev->screen, st_ctx->real_pipe);
151 if(!st_ctx->pipe) {
152 st_context_destroy(st_ctx);
153 return NULL;
154 }
155
156 st_ctx->cso = cso_create_context(st_ctx->pipe);
157 if(!st_ctx->cso) {
158 st_context_destroy(st_ctx);
159 return NULL;
160 }
161
162 /* disabled blending/masking */
163 {
164 struct pipe_blend_state blend;
165 memset(&blend, 0, sizeof(blend));
166 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
167 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
168 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
169 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
170 blend.colormask = PIPE_MASK_RGBA;
171 cso_set_blend(st_ctx->cso, &blend);
172 }
173
174 /* no-op depth/stencil/alpha */
175 {
176 struct pipe_depth_stencil_alpha_state depthstencil;
177 memset(&depthstencil, 0, sizeof(depthstencil));
178 cso_set_depth_stencil_alpha(st_ctx->cso, &depthstencil);
179 }
180
181 /* rasterizer */
182 {
183 struct pipe_rasterizer_state rasterizer;
184 memset(&rasterizer, 0, sizeof(rasterizer));
185 rasterizer.front_winding = PIPE_WINDING_CW;
186 rasterizer.cull_mode = PIPE_WINDING_NONE;
187 rasterizer.bypass_clipping = 1;
188 /*rasterizer.bypass_vs = 1;*/
189 cso_set_rasterizer(st_ctx->cso, &rasterizer);
190 }
191
192 /* identity viewport */
193 {
194 struct pipe_viewport_state viewport;
195 viewport.scale[0] = 1.0;
196 viewport.scale[1] = 1.0;
197 viewport.scale[2] = 1.0;
198 viewport.scale[3] = 1.0;
199 viewport.translate[0] = 0.0;
200 viewport.translate[1] = 0.0;
201 viewport.translate[2] = 0.0;
202 viewport.translate[3] = 0.0;
203 cso_set_viewport(st_ctx->cso, &viewport);
204 }
205
206 /* samplers */
207 {
208 struct pipe_sampler_state sampler;
209 unsigned i;
210 memset(&sampler, 0, sizeof(sampler));
211 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
212 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
213 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
214 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
215 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
216 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
217 sampler.normalized_coords = 1;
218 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
219 cso_single_sampler(st_ctx->cso, i, &sampler);
220 cso_single_sampler_done(st_ctx->cso);
221 }
222
223 /* default textures */
224 {
225 struct pipe_screen *screen = st_dev->screen;
226 struct pipe_texture templat;
227 struct pipe_surface *surface;
228 unsigned i;
229
230 memset( &templat, 0, sizeof( templat ) );
231 templat.target = PIPE_TEXTURE_2D;
232 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
233 templat.block.size = 4;
234 templat.block.width = 1;
235 templat.block.height = 1;
236 templat.width[0] = 1;
237 templat.height[0] = 1;
238 templat.depth[0] = 1;
239 templat.last_level = 0;
240
241 st_ctx->default_texture = screen->texture_create( screen, &templat );
242 if(st_ctx->default_texture) {
243 surface = screen->get_tex_surface( screen,
244 st_ctx->default_texture, 0, 0, 0,
245 PIPE_BUFFER_USAGE_CPU_WRITE );
246 if(surface) {
247 uint32_t *map;
248 map = (uint32_t *) pipe_surface_map(surface, PIPE_BUFFER_USAGE_CPU_WRITE );
249 if(map) {
250 *map = 0x00000000;
251 pipe_surface_unmap( surface );
252 }
253 pipe_surface_reference(&surface, NULL);
254 }
255 }
256
257 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
258 pipe_texture_reference(&st_ctx->sampler_textures[i], st_ctx->default_texture);
259
260 cso_set_sampler_textures(st_ctx->cso, PIPE_MAX_SAMPLERS, st_ctx->sampler_textures);
261 }
262
263 /* vertex shader */
264 {
265 struct pipe_shader_state vert_shader;
266
267 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
268 TGSI_SEMANTIC_GENERIC };
269 const uint semantic_indexes[] = { 0, 0 };
270 st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe,
271 2,
272 semantic_names,
273 semantic_indexes,
274 &vert_shader);
275 cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
276 }
277
278 /* fragment shader */
279 {
280 struct pipe_shader_state frag_shader;
281 st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe,
282 &frag_shader);
283 cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
284 }
285
286 return st_ctx;
287 }
288
289
290 void
291 st_buffer_destroy(struct st_buffer *st_buf)
292 {
293 if(st_buf) {
294 struct pipe_winsys *winsys = st_buf->st_dev->screen->winsys;
295 pipe_buffer_reference(winsys, &st_buf->buffer, NULL);
296 FREE(st_buf);
297 }
298 }
299
300
301 struct st_buffer *
302 st_buffer_create(struct st_device *st_dev,
303 unsigned alignment, unsigned usage, unsigned size)
304 {
305 struct pipe_winsys *winsys = st_dev->screen->winsys;
306 struct st_buffer *st_buf;
307
308 st_buf = CALLOC_STRUCT(st_buffer);
309 if(!st_buf)
310 return NULL;
311
312 st_buf->st_dev = st_dev;
313
314 st_buf->buffer = winsys->buffer_create(winsys, alignment, usage, size);
315 if(!st_buf->buffer) {
316 st_buffer_destroy(st_buf);
317 return NULL;
318 }
319
320 return st_buf;
321 }
322