Merge branch '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_math.h"
35 #include "util/u_memory.h"
36 #include "util/u_simple_shaders.h"
37 #include "trace/tr_public.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 /* FIXME: Don't really destroy until we keep track of every single
48 * reference or we end up causing a segmentation fault every time
49 * python exits. */
50 #if 0
51 st_dev->screen->destroy(st_dev->screen);
52 #endif
53 }
54
55 FREE(st_dev);
56 }
57
58
59 static void
60 st_device_reference(struct st_device **ptr, struct st_device *st_dev)
61 {
62 struct st_device *old_dev = *ptr;
63
64 if (pipe_reference(&(*ptr)->reference, &st_dev->reference))
65 st_device_really_destroy(old_dev);
66 *ptr = st_dev;
67 }
68
69
70 void
71 st_device_destroy(struct st_device *st_dev)
72 {
73 st_device_reference(&st_dev, NULL);
74 }
75
76
77 struct st_device *
78 st_device_create(boolean hardware)
79 {
80 struct pipe_screen *screen;
81 struct st_device *st_dev;
82
83 if (hardware)
84 screen = st_hardware_screen_create();
85 else
86 screen = st_software_screen_create();
87
88 screen = trace_screen_create(screen);
89 if (!screen)
90 goto no_screen;
91
92 st_dev = CALLOC_STRUCT(st_device);
93 if (!st_dev)
94 goto no_device;
95
96 pipe_reference_init(&st_dev->reference, 1);
97 st_dev->screen = screen;
98
99 return st_dev;
100
101 no_device:
102 screen->destroy(screen);
103 no_screen:
104 return NULL;
105 }
106
107
108 void
109 st_context_destroy(struct st_context *st_ctx)
110 {
111 unsigned i;
112
113 if(st_ctx) {
114 struct st_device *st_dev = st_ctx->st_dev;
115
116 if(st_ctx->cso) {
117 cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
118 cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
119
120 cso_destroy_context(st_ctx->cso);
121 }
122
123 if(st_ctx->pipe)
124 st_ctx->pipe->destroy(st_ctx->pipe);
125
126 for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
127 pipe_texture_reference(&st_ctx->fragment_sampler_textures[i], NULL);
128 for(i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
129 pipe_texture_reference(&st_ctx->vertex_sampler_textures[i], NULL);
130 pipe_texture_reference(&st_ctx->default_texture, NULL);
131
132 FREE(st_ctx);
133
134 st_device_reference(&st_dev, NULL);
135 }
136 }
137
138
139 struct st_context *
140 st_context_create(struct st_device *st_dev)
141 {
142 struct st_context *st_ctx;
143
144 st_ctx = CALLOC_STRUCT(st_context);
145 if(!st_ctx)
146 return NULL;
147
148 st_device_reference(&st_ctx->st_dev, st_dev);
149
150 st_ctx->pipe = st_dev->screen->context_create(st_dev->screen, NULL);
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.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
167 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
168 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
169 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
170 blend.rt[0].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 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_screen *screen = st_dev->screen;
231 struct pipe_texture templat;
232 struct pipe_transfer *transfer;
233 unsigned i;
234
235 memset( &templat, 0, sizeof( templat ) );
236 templat.target = PIPE_TEXTURE_2D;
237 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
238 templat.width0 = 1;
239 templat.height0 = 1;
240 templat.depth0 = 1;
241 templat.last_level = 0;
242
243 st_ctx->default_texture = screen->texture_create( screen, &templat );
244 if(st_ctx->default_texture) {
245 transfer = screen->get_tex_transfer(screen,
246 st_ctx->default_texture,
247 0, 0, 0,
248 PIPE_TRANSFER_WRITE,
249 0, 0,
250 st_ctx->default_texture->width0,
251 st_ctx->default_texture->height0);
252 if (transfer) {
253 uint32_t *map;
254 map = (uint32_t *) screen->transfer_map(screen, transfer);
255 if(map) {
256 *map = 0x00000000;
257 screen->transfer_unmap(screen, transfer);
258 }
259 screen->tex_transfer_destroy(transfer);
260 }
261 }
262
263 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
264 pipe_texture_reference(&st_ctx->fragment_sampler_textures[i], st_ctx->default_texture);
265 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++)
266 pipe_texture_reference(&st_ctx->vertex_sampler_textures[i], st_ctx->default_texture);
267
268 cso_set_sampler_textures(st_ctx->cso, PIPE_MAX_SAMPLERS, st_ctx->fragment_sampler_textures);
269 cso_set_vertex_sampler_textures(st_ctx->cso, PIPE_MAX_VERTEX_SAMPLERS, st_ctx->vertex_sampler_textures);
270 }
271
272 /* vertex shader */
273 {
274 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
275 TGSI_SEMANTIC_GENERIC };
276 const uint semantic_indexes[] = { 0, 0 };
277 st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe,
278 2,
279 semantic_names,
280 semantic_indexes);
281 cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
282 }
283
284 /* fragment shader */
285 {
286 st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe);
287 cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
288 }
289
290 return st_ctx;
291 }