Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[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 "st_device.h"
37 #include "st_winsys.h"
38
39
40 static void
41 st_device_really_destroy(struct st_device *st_dev)
42 {
43 if(st_dev->screen)
44 st_dev->st_ws->screen_destroy(st_dev->screen);
45
46 FREE(st_dev);
47 }
48
49
50 void
51 st_device_destroy(struct st_device *st_dev)
52 {
53 if(!--st_dev->refcount)
54 st_device_really_destroy(st_dev);
55 }
56
57
58 static struct st_device *
59 st_device_create_from_st_winsys(const struct st_winsys *st_ws)
60 {
61 struct st_device *st_dev;
62
63 if(!st_ws->screen_create ||
64 !st_ws->screen_destroy ||
65 !st_ws->context_create ||
66 !st_ws->context_destroy)
67 return NULL;
68
69 st_dev = CALLOC_STRUCT(st_device);
70 if(!st_dev)
71 return NULL;
72
73 st_dev->refcount = 1;
74 st_dev->st_ws = st_ws;
75
76 st_dev->screen = st_ws->screen_create();
77 if(!st_dev->screen)
78 st_device_destroy(st_dev);
79
80 return st_dev;
81 }
82
83
84 struct st_device *
85 st_device_create(boolean hardware) {
86 if(hardware)
87 return st_device_create_from_st_winsys(&st_hardpipe_winsys);
88 else
89 return st_device_create_from_st_winsys(&st_softpipe_winsys);
90 }
91
92
93 void
94 st_context_destroy(struct st_context *st_ctx)
95 {
96 unsigned i;
97
98 if(st_ctx) {
99 struct st_device *st_dev = st_ctx->st_dev;
100
101 if(st_ctx->cso) {
102 cso_delete_vertex_shader(st_ctx->cso, st_ctx->vs);
103 cso_delete_fragment_shader(st_ctx->cso, st_ctx->fs);
104
105 cso_destroy_context(st_ctx->cso);
106 }
107
108 if(st_ctx->pipe)
109 st_ctx->st_dev->st_ws->context_destroy(st_ctx->pipe);
110
111 for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
112 pipe_texture_reference(&st_ctx->sampler_textures[i], NULL);
113 pipe_texture_reference(&st_ctx->default_texture, NULL);
114
115 FREE(st_ctx);
116
117 if(!--st_dev->refcount)
118 st_device_really_destroy(st_dev);
119 }
120 }
121
122
123 struct st_context *
124 st_context_create(struct st_device *st_dev)
125 {
126 struct st_context *st_ctx;
127
128 st_ctx = CALLOC_STRUCT(st_context);
129 if(!st_ctx)
130 return NULL;
131
132 st_ctx->st_dev = st_dev;
133 ++st_dev->refcount;
134
135 st_ctx->pipe = st_dev->st_ws->context_create(st_dev->screen);
136 if(!st_ctx->pipe)
137 st_context_destroy(st_ctx);
138
139 st_ctx->cso = cso_create_context(st_ctx->pipe);
140 if(!st_ctx->cso)
141 st_context_destroy(st_ctx);
142
143 /* disabled blending/masking */
144 {
145 struct pipe_blend_state blend;
146 memset(&blend, 0, sizeof(blend));
147 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
148 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
149 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
150 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
151 blend.colormask = PIPE_MASK_RGBA;
152 cso_set_blend(st_ctx->cso, &blend);
153 }
154
155 /* no-op depth/stencil/alpha */
156 {
157 struct pipe_depth_stencil_alpha_state depthstencil;
158 memset(&depthstencil, 0, sizeof(depthstencil));
159 cso_set_depth_stencil_alpha(st_ctx->cso, &depthstencil);
160 }
161
162 /* rasterizer */
163 {
164 struct pipe_rasterizer_state rasterizer;
165 memset(&rasterizer, 0, sizeof(rasterizer));
166 rasterizer.front_winding = PIPE_WINDING_CW;
167 rasterizer.cull_mode = PIPE_WINDING_NONE;
168 rasterizer.bypass_clipping = 1;
169 /*rasterizer.bypass_vs = 1;*/
170 cso_set_rasterizer(st_ctx->cso, &rasterizer);
171 }
172
173 /* identity viewport */
174 {
175 struct pipe_viewport_state viewport;
176 viewport.scale[0] = 1.0;
177 viewport.scale[1] = 1.0;
178 viewport.scale[2] = 1.0;
179 viewport.scale[3] = 1.0;
180 viewport.translate[0] = 0.0;
181 viewport.translate[1] = 0.0;
182 viewport.translate[2] = 0.0;
183 viewport.translate[3] = 0.0;
184 cso_set_viewport(st_ctx->cso, &viewport);
185 }
186
187 /* samplers */
188 {
189 struct pipe_sampler_state sampler;
190 unsigned i;
191 memset(&sampler, 0, sizeof(sampler));
192 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
193 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
194 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
195 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
196 sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
197 sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
198 sampler.normalized_coords = 1;
199 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
200 cso_single_sampler(st_ctx->cso, i, &sampler);
201 cso_single_sampler_done(st_ctx->cso);
202 }
203
204 /* default textures */
205 {
206 struct pipe_screen *screen = st_dev->screen;
207 struct pipe_texture templat;
208 struct pipe_surface *surface;
209 unsigned i;
210
211 memset( &templat, 0, sizeof( templat ) );
212 templat.target = PIPE_TEXTURE_2D;
213 templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
214 templat.block.size = 4;
215 templat.block.width = 1;
216 templat.block.height = 1;
217 templat.width[0] = 1;
218 templat.height[0] = 1;
219 templat.depth[0] = 1;
220 templat.last_level = 0;
221
222 st_ctx->default_texture = screen->texture_create( screen, &templat );
223 if(st_ctx->default_texture) {
224 surface = screen->get_tex_surface( screen,
225 st_ctx->default_texture, 0, 0, 0,
226 PIPE_BUFFER_USAGE_CPU_WRITE );
227 if(surface) {
228 uint32_t *map;
229 map = (uint32_t *) pipe_surface_map(surface, PIPE_BUFFER_USAGE_CPU_WRITE );
230 if(map) {
231 *map = 0x00000000;
232 pipe_surface_unmap( surface );
233 }
234 pipe_surface_reference(&surface, NULL);
235 }
236 }
237
238 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
239 pipe_texture_reference(&st_ctx->sampler_textures[i], st_ctx->default_texture);
240
241 cso_set_sampler_textures(st_ctx->cso, PIPE_MAX_SAMPLERS, st_ctx->sampler_textures);
242 }
243
244 /* vertex shader */
245 {
246 struct pipe_shader_state vert_shader;
247
248 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
249 TGSI_SEMANTIC_GENERIC };
250 const uint semantic_indexes[] = { 0, 0 };
251 st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe,
252 2,
253 semantic_names,
254 semantic_indexes,
255 &vert_shader);
256 cso_set_vertex_shader_handle(st_ctx->cso, st_ctx->vs);
257 }
258
259 /* fragment shader */
260 {
261 struct pipe_shader_state frag_shader;
262 st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe,
263 &frag_shader);
264 cso_set_fragment_shader_handle(st_ctx->cso, st_ctx->fs);
265 }
266
267 return st_ctx;
268 }