gallium: remove trace module injection from various state trackers
[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
38 #include "st_device.h"
39 #include "st_winsys.h"
40
41
42 static void
43 st_device_really_destroy(struct st_device *st_dev)
44 {
45 if(st_dev->screen) {
46 /* FIXME: Don't really destroy until we keep track of every single
47 * reference or we end up causing a segmentation fault every time
48 * python exits. */
49 #if 0
50 st_dev->screen->destroy(st_dev->screen);
51 #endif
52 }
53
54 FREE(st_dev);
55 }
56
57
58 static void
59 st_device_reference(struct st_device **ptr, struct st_device *st_dev)
60 {
61 struct st_device *old_dev = *ptr;
62
63 if (pipe_reference(&(*ptr)->reference, &st_dev->reference))
64 st_device_really_destroy(old_dev);
65 *ptr = st_dev;
66 }
67
68
69 void
70 st_device_destroy(struct st_device *st_dev)
71 {
72 st_device_reference(&st_dev, NULL);
73 }
74
75
76 struct st_device *
77 st_device_create(boolean hardware)
78 {
79 struct pipe_screen *screen;
80 struct st_device *st_dev;
81
82 if (hardware)
83 screen = st_hardware_screen_create();
84 else
85 screen = st_software_screen_create();
86
87 st_dev = CALLOC_STRUCT(st_device);
88 if (!st_dev)
89 goto no_device;
90
91 pipe_reference_init(&st_dev->reference, 1);
92 st_dev->screen = screen;
93
94 return st_dev;
95
96 no_device:
97 screen->destroy(screen);
98 no_screen:
99 return NULL;
100 }
101
102
103 void
104 st_context_destroy(struct st_context *st_ctx)
105 {
106 unsigned i;
107
108 if(st_ctx) {
109 struct st_device *st_dev = st_ctx->st_dev;
110
111 if(st_ctx->cso) {
112 cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
113 cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
114
115 cso_destroy_context(st_ctx->cso);
116 }
117
118 if(st_ctx->pipe)
119 st_ctx->pipe->destroy(st_ctx->pipe);
120
121 for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
122 pipe_texture_reference(&st_ctx->fragment_sampler_textures[i], NULL);
123 for(i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
124 pipe_texture_reference(&st_ctx->vertex_sampler_textures[i], NULL);
125 pipe_texture_reference(&st_ctx->default_texture, NULL);
126
127 FREE(st_ctx);
128
129 st_device_reference(&st_dev, NULL);
130 }
131 }
132
133
134 struct st_context *
135 st_context_create(struct st_device *st_dev)
136 {
137 struct st_context *st_ctx;
138
139 st_ctx = CALLOC_STRUCT(st_context);
140 if(!st_ctx)
141 return NULL;
142
143 st_device_reference(&st_ctx->st_dev, st_dev);
144
145 st_ctx->pipe = st_dev->screen->context_create(st_dev->screen, NULL);
146 if(!st_ctx->pipe) {
147 st_context_destroy(st_ctx);
148 return NULL;
149 }
150
151 st_ctx->cso = cso_create_context(st_ctx->pipe);
152 if(!st_ctx->cso) {
153 st_context_destroy(st_ctx);
154 return NULL;
155 }
156
157 /* disabled blending/masking */
158 {
159 struct pipe_blend_state blend;
160 memset(&blend, 0, sizeof(blend));
161 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
162 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
163 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
164 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
165 blend.rt[0].colormask = PIPE_MASK_RGBA;
166 cso_set_blend(st_ctx->cso, &blend);
167 }
168
169 /* no-op depth/stencil/alpha */
170 {
171 struct pipe_depth_stencil_alpha_state depthstencil;
172 memset(&depthstencil, 0, sizeof(depthstencil));
173 cso_set_depth_stencil_alpha(st_ctx->cso, &depthstencil);
174 }
175
176 /* rasterizer */
177 {
178 struct pipe_rasterizer_state rasterizer;
179 memset(&rasterizer, 0, sizeof(rasterizer));
180 rasterizer.front_winding = PIPE_WINDING_CW;
181 rasterizer.cull_mode = PIPE_WINDING_NONE;
182 cso_set_rasterizer(st_ctx->cso, &rasterizer);
183 }
184
185 /* clip */
186 {
187 struct pipe_clip_state clip;
188 memset(&clip, 0, sizeof(clip));
189 st_ctx->pipe->set_clip_state(st_ctx->pipe, &clip);
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_transfer *transfer;
228 unsigned i;
229
230 memset( &templat, 0, sizeof( templat ) );
231 templat.target = PIPE_TEXTURE_2D;
232 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
233 templat.width0 = 1;
234 templat.height0 = 1;
235 templat.depth0 = 1;
236 templat.last_level = 0;
237
238 st_ctx->default_texture = screen->texture_create( screen, &templat );
239 if(st_ctx->default_texture) {
240 transfer = screen->get_tex_transfer(screen,
241 st_ctx->default_texture,
242 0, 0, 0,
243 PIPE_TRANSFER_WRITE,
244 0, 0,
245 st_ctx->default_texture->width0,
246 st_ctx->default_texture->height0);
247 if (transfer) {
248 uint32_t *map;
249 map = (uint32_t *) screen->transfer_map(screen, transfer);
250 if(map) {
251 *map = 0x00000000;
252 screen->transfer_unmap(screen, transfer);
253 }
254 screen->tex_transfer_destroy(transfer);
255 }
256 }
257
258 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
259 pipe_texture_reference(&st_ctx->fragment_sampler_textures[i], st_ctx->default_texture);
260 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++)
261 pipe_texture_reference(&st_ctx->vertex_sampler_textures[i], st_ctx->default_texture);
262
263 cso_set_sampler_textures(st_ctx->cso, PIPE_MAX_SAMPLERS, st_ctx->fragment_sampler_textures);
264 cso_set_vertex_sampler_textures(st_ctx->cso, PIPE_MAX_VERTEX_SAMPLERS, st_ctx->vertex_sampler_textures);
265 }
266
267 /* vertex shader */
268 {
269 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
270 TGSI_SEMANTIC_GENERIC };
271 const uint semantic_indexes[] = { 0, 0 };
272 st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe,
273 2,
274 semantic_names,
275 semantic_indexes);
276 cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
277 }
278
279 /* fragment shader */
280 {
281 st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe);
282 cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
283 }
284
285 return st_ctx;
286 }