1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
40 #include "st_device.h"
41 #include "st_winsys.h"
45 st_device_really_destroy(struct st_device
*st_dev
)
48 /* FIXME: Don't really destroy until we keep track of every single
49 * reference or we end up causing a segmentation fault every time
52 st_dev
->screen
->destroy(st_dev
->screen
);
61 st_device_reference(struct st_device
**ptr
, struct st_device
*st_dev
)
63 struct st_device
*old_dev
= *ptr
;
65 if (pipe_reference((struct pipe_reference
**)ptr
, &st_dev
->reference
))
66 st_device_really_destroy(old_dev
);
71 st_device_destroy(struct st_device
*st_dev
)
73 st_device_reference(&st_dev
, NULL
);
77 static struct st_device
*
78 st_device_create_from_st_winsys(const struct st_winsys
*st_ws
)
80 struct st_device
*st_dev
;
82 if(!st_ws
->screen_create
||
83 !st_ws
->context_create
)
86 st_dev
= CALLOC_STRUCT(st_device
);
90 pipe_reference_init(&st_dev
->reference
, 1);
91 st_dev
->st_ws
= st_ws
;
93 st_dev
->real_screen
= st_ws
->screen_create();
94 if(!st_dev
->real_screen
) {
95 st_device_destroy(st_dev
);
99 st_dev
->screen
= trace_screen_create(st_dev
->real_screen
);
100 if(!st_dev
->screen
) {
101 st_device_destroy(st_dev
);
110 st_device_create(boolean hardware
) {
112 return st_device_create_from_st_winsys(&st_hardpipe_winsys
);
114 return st_device_create_from_st_winsys(&st_softpipe_winsys
);
119 st_context_destroy(struct st_context
*st_ctx
)
124 struct st_device
*st_dev
= st_ctx
->st_dev
;
127 cso_delete_vertex_shader(st_ctx
->cso
, st_ctx
->vs
);
128 cso_delete_fragment_shader(st_ctx
->cso
, st_ctx
->fs
);
130 cso_destroy_context(st_ctx
->cso
);
134 st_ctx
->pipe
->destroy(st_ctx
->pipe
);
136 for(i
= 0; i
< PIPE_MAX_SAMPLERS
; ++i
)
137 pipe_texture_reference(&st_ctx
->sampler_textures
[i
], NULL
);
138 pipe_texture_reference(&st_ctx
->default_texture
, NULL
);
142 st_device_reference(&st_dev
, NULL
);
148 st_context_create(struct st_device
*st_dev
)
150 struct st_context
*st_ctx
;
152 st_ctx
= CALLOC_STRUCT(st_context
);
156 st_device_reference(&st_ctx
->st_dev
, st_dev
);
158 st_ctx
->real_pipe
= st_dev
->st_ws
->context_create(st_dev
->real_screen
);
159 if(!st_ctx
->real_pipe
) {
160 st_context_destroy(st_ctx
);
164 st_ctx
->pipe
= trace_context_create(st_dev
->screen
, st_ctx
->real_pipe
);
166 st_context_destroy(st_ctx
);
170 st_ctx
->cso
= cso_create_context(st_ctx
->pipe
);
172 st_context_destroy(st_ctx
);
176 /* disabled blending/masking */
178 struct pipe_blend_state blend
;
179 memset(&blend
, 0, sizeof(blend
));
180 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
181 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
182 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
183 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
184 blend
.colormask
= PIPE_MASK_RGBA
;
185 cso_set_blend(st_ctx
->cso
, &blend
);
188 /* no-op depth/stencil/alpha */
190 struct pipe_depth_stencil_alpha_state depthstencil
;
191 memset(&depthstencil
, 0, sizeof(depthstencil
));
192 cso_set_depth_stencil_alpha(st_ctx
->cso
, &depthstencil
);
197 struct pipe_rasterizer_state rasterizer
;
198 memset(&rasterizer
, 0, sizeof(rasterizer
));
199 rasterizer
.front_winding
= PIPE_WINDING_CW
;
200 rasterizer
.cull_mode
= PIPE_WINDING_NONE
;
201 cso_set_rasterizer(st_ctx
->cso
, &rasterizer
);
206 struct pipe_clip_state clip
;
207 memset(&clip
, 0, sizeof(clip
));
208 st_ctx
->pipe
->set_clip_state(st_ctx
->pipe
, &clip
);
211 /* identity viewport */
213 struct pipe_viewport_state viewport
;
214 viewport
.scale
[0] = 1.0;
215 viewport
.scale
[1] = 1.0;
216 viewport
.scale
[2] = 1.0;
217 viewport
.scale
[3] = 1.0;
218 viewport
.translate
[0] = 0.0;
219 viewport
.translate
[1] = 0.0;
220 viewport
.translate
[2] = 0.0;
221 viewport
.translate
[3] = 0.0;
222 cso_set_viewport(st_ctx
->cso
, &viewport
);
227 struct pipe_sampler_state sampler
;
229 memset(&sampler
, 0, sizeof(sampler
));
230 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
231 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
232 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
233 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
234 sampler
.min_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
235 sampler
.mag_img_filter
= PIPE_TEX_MIPFILTER_NEAREST
;
236 sampler
.normalized_coords
= 1;
237 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++)
238 cso_single_sampler(st_ctx
->cso
, i
, &sampler
);
239 cso_single_sampler_done(st_ctx
->cso
);
242 /* default textures */
244 struct pipe_screen
*screen
= st_dev
->screen
;
245 struct pipe_texture templat
;
246 struct pipe_transfer
*transfer
;
249 memset( &templat
, 0, sizeof( templat
) );
250 templat
.target
= PIPE_TEXTURE_2D
;
251 templat
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
252 templat
.block
.size
= 4;
253 templat
.block
.width
= 1;
254 templat
.block
.height
= 1;
258 templat
.last_level
= 0;
260 st_ctx
->default_texture
= screen
->texture_create( screen
, &templat
);
261 if(st_ctx
->default_texture
) {
262 transfer
= screen
->get_tex_transfer(screen
,
263 st_ctx
->default_texture
,
267 st_ctx
->default_texture
->width0
,
268 st_ctx
->default_texture
->height0
);
271 map
= (uint32_t *) screen
->transfer_map(screen
, transfer
);
274 screen
->transfer_unmap(screen
, transfer
);
276 screen
->tex_transfer_destroy(transfer
);
280 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++)
281 pipe_texture_reference(&st_ctx
->sampler_textures
[i
], st_ctx
->default_texture
);
283 cso_set_sampler_textures(st_ctx
->cso
, PIPE_MAX_SAMPLERS
, st_ctx
->sampler_textures
);
288 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
289 TGSI_SEMANTIC_GENERIC
};
290 const uint semantic_indexes
[] = { 0, 0 };
291 st_ctx
->vs
= util_make_vertex_passthrough_shader(st_ctx
->pipe
,
295 cso_set_vertex_shader_handle(st_ctx
->cso
, st_ctx
->vs
);
298 /* fragment shader */
300 st_ctx
->fs
= util_make_fragment_passthrough_shader(st_ctx
->pipe
);
301 cso_set_fragment_shader_handle(st_ctx
->cso
, st_ctx
->fs
);