python: Use transfer objects to initialise texture data.
[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 "pipe/p_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_screen.h"
38 #include "trace/tr_context.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 st_dev->screen->destroy(st_dev->screen);
49
50 FREE(st_dev);
51 }
52
53
54 static void
55 st_device_reference(struct st_device **ptr, struct st_device *st_dev)
56 {
57 struct st_device *old_dev = *ptr;
58
59 if (pipe_reference((struct pipe_reference **)ptr, &st_dev->reference))
60 st_device_really_destroy(old_dev);
61 }
62
63
64 void
65 st_device_destroy(struct st_device *st_dev)
66 {
67 st_device_reference(&st_dev, NULL);
68 }
69
70
71 static struct st_device *
72 st_device_create_from_st_winsys(const struct st_winsys *st_ws)
73 {
74 struct st_device *st_dev;
75
76 if(!st_ws->screen_create ||
77 !st_ws->context_create)
78 return NULL;
79
80 st_dev = CALLOC_STRUCT(st_device);
81 if(!st_dev)
82 return NULL;
83
84 pipe_reference_init(&st_dev->reference, 1);
85 st_dev->st_ws = st_ws;
86
87 st_dev->real_screen = st_ws->screen_create();
88 if(!st_dev->real_screen) {
89 st_device_destroy(st_dev);
90 return NULL;
91 }
92
93 st_dev->screen = trace_screen_create(st_dev->real_screen);
94 if(!st_dev->screen) {
95 st_device_destroy(st_dev);
96 return NULL;
97 }
98
99 return st_dev;
100 }
101
102
103 struct st_device *
104 st_device_create(boolean hardware) {
105 if(hardware)
106 return st_device_create_from_st_winsys(&st_hardpipe_winsys);
107 else
108 return st_device_create_from_st_winsys(&st_softpipe_winsys);
109 }
110
111
112 void
113 st_context_destroy(struct st_context *st_ctx)
114 {
115 unsigned i;
116
117 if(st_ctx) {
118 struct st_device *st_dev = st_ctx->st_dev;
119
120 if(st_ctx->cso) {
121 cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
122 cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
123
124 cso_destroy_context(st_ctx->cso);
125 }
126
127 if(st_ctx->pipe)
128 st_ctx->pipe->destroy(st_ctx->pipe);
129
130 for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
131 pipe_texture_reference(&st_ctx->sampler_textures[i], NULL);
132 pipe_texture_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->real_pipe = st_dev->st_ws->context_create(st_dev->real_screen);
153 if(!st_ctx->real_pipe) {
154 st_context_destroy(st_ctx);
155 return NULL;
156 }
157
158 st_ctx->pipe = trace_context_create(st_dev->screen, st_ctx->real_pipe);
159 if(!st_ctx->pipe) {
160 st_context_destroy(st_ctx);
161 return NULL;
162 }
163
164 st_ctx->cso = cso_create_context(st_ctx->pipe);
165 if(!st_ctx->cso) {
166 st_context_destroy(st_ctx);
167 return NULL;
168 }
169
170 /* disabled blending/masking */
171 {
172 struct pipe_blend_state blend;
173 memset(&blend, 0, sizeof(blend));
174 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
175 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
176 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
177 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
178 blend.colormask = PIPE_MASK_RGBA;
179 cso_set_blend(st_ctx->cso, &blend);
180 }
181
182 /* no-op depth/stencil/alpha */
183 {
184 struct pipe_depth_stencil_alpha_state depthstencil;
185 memset(&depthstencil, 0, sizeof(depthstencil));
186 cso_set_depth_stencil_alpha(st_ctx->cso, &depthstencil);
187 }
188
189 /* rasterizer */
190 {
191 struct pipe_rasterizer_state rasterizer;
192 memset(&rasterizer, 0, sizeof(rasterizer));
193 rasterizer.front_winding = PIPE_WINDING_CW;
194 rasterizer.cull_mode = PIPE_WINDING_NONE;
195 rasterizer.bypass_vs_clip_and_viewport = 1;
196 cso_set_rasterizer(st_ctx->cso, &rasterizer);
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_screen *screen = st_dev->screen;
233 struct pipe_texture templat;
234 struct pipe_transfer *transfer;
235 unsigned i;
236
237 memset( &templat, 0, sizeof( templat ) );
238 templat.target = PIPE_TEXTURE_2D;
239 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
240 templat.block.size = 4;
241 templat.block.width = 1;
242 templat.block.height = 1;
243 templat.width[0] = 1;
244 templat.height[0] = 1;
245 templat.depth[0] = 1;
246 templat.last_level = 0;
247
248 st_ctx->default_texture = screen->texture_create( screen, &templat );
249 if(st_ctx->default_texture) {
250 transfer = screen->get_tex_transfer(screen,
251 st_ctx->default_texture,
252 0, 0, 0,
253 PIPE_TRANSFER_WRITE,
254 0, 0,
255 st_ctx->default_texture->width[0],
256 st_ctx->default_texture->height[0]);
257 if (transfer) {
258 uint32_t *map;
259 map = (uint32_t *) screen->transfer_map(screen, transfer);
260 if(map) {
261 *map = 0x00000000;
262 screen->transfer_unmap(screen, transfer);
263 }
264 screen->tex_transfer_destroy(transfer);
265 }
266 }
267
268 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
269 pipe_texture_reference(&st_ctx->sampler_textures[i], st_ctx->default_texture);
270
271 cso_set_sampler_textures(st_ctx->cso, PIPE_MAX_SAMPLERS, st_ctx->sampler_textures);
272 }
273
274 /* vertex shader */
275 {
276 struct pipe_shader_state vert_shader;
277
278 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
279 TGSI_SEMANTIC_GENERIC };
280 const uint semantic_indexes[] = { 0, 0 };
281 st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe,
282 2,
283 semantic_names,
284 semantic_indexes,
285 &vert_shader);
286 cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
287 }
288
289 /* fragment shader */
290 {
291 struct pipe_shader_state frag_shader;
292 st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe,
293 &frag_shader);
294 cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
295 }
296
297 return st_ctx;
298 }
299
300
301 void
302 st_buffer_destroy(struct st_buffer *st_buf)
303 {
304 if(st_buf) {
305 pipe_buffer_reference(&st_buf->buffer, NULL);
306 FREE(st_buf);
307 }
308 }
309
310
311 struct st_buffer *
312 st_buffer_create(struct st_device *st_dev,
313 unsigned alignment, unsigned usage, unsigned size)
314 {
315 struct pipe_screen *screen = st_dev->screen;
316 struct st_buffer *st_buf;
317
318 st_buf = CALLOC_STRUCT(st_buffer);
319 if(!st_buf)
320 return NULL;
321
322 st_buf->st_dev = st_dev;
323
324 st_buf->buffer = pipe_buffer_create(screen, alignment, usage, size);
325 if(!st_buf->buffer) {
326 st_buffer_destroy(st_buf);
327 return NULL;
328 }
329
330 return st_buf;
331 }
332