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->st_ws = st_ws;
74
75 st_dev->screen = st_ws->screen_create();
76 if(!st_dev->screen)
77 st_device_destroy(st_dev);
78
79 return st_dev;
80 }
81
82
83 struct st_device *
84 st_device_create(boolean hardware) {
85 #if 0
86 if(hardware)
87 return st_device_create_from_st_winsys(&st_hardware_winsys);
88 else
89 #endif
90 return st_device_create_from_st_winsys(&st_software_winsys);
91 }
92
93
94 void
95 st_context_destroy(struct st_context *st_ctx)
96 {
97 unsigned i;
98
99 if(st_ctx) {
100 struct st_device *st_dev = st_ctx->st_dev;
101
102 if(st_ctx->vs) {
103 st_ctx->pipe->bind_vs_state(st_ctx->pipe, NULL);
104 st_ctx->pipe->delete_vs_state(st_ctx->pipe, st_ctx->vs);
105 }
106
107 if(st_ctx->fs) {
108 st_ctx->pipe->bind_fs_state(st_ctx->pipe, NULL);
109 st_ctx->pipe->delete_fs_state(st_ctx->pipe, st_ctx->fs);
110 }
111
112 if(st_ctx->cso)
113 cso_destroy_context(st_ctx->cso);
114
115 if(st_ctx->pipe)
116 st_ctx->st_dev->st_ws->context_destroy(st_ctx->pipe);
117
118 for(i = 0; i < PIPE_MAX_SAMPLERS; ++i)
119 pipe_texture_reference(&st_ctx->sampler_textures[i], NULL);
120
121 FREE(st_ctx);
122
123 if(!--st_dev->refcount)
124 st_device_really_destroy(st_dev);
125 }
126 }
127
128
129 struct st_context *
130 st_context_create(struct st_device *st_dev)
131 {
132 struct st_context *st_ctx;
133
134 st_ctx = CALLOC_STRUCT(st_context);
135 if(!st_ctx)
136 return NULL;
137
138 st_ctx->st_dev = st_dev;
139 ++st_dev->refcount;
140
141 st_ctx->pipe = st_dev->st_ws->context_create(st_dev->screen);
142 if(!st_ctx->pipe)
143 st_context_destroy(st_ctx);
144
145 st_ctx->cso = cso_create_context(st_ctx->pipe);
146 if(!st_ctx->cso)
147 st_context_destroy(st_ctx);
148
149 /* vertex shader */
150 {
151 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
152 TGSI_SEMANTIC_GENERIC };
153 const uint semantic_indexes[] = { 0, 0 };
154 st_ctx->vs = util_make_vertex_passthrough_shader(st_ctx->pipe,
155 2,
156 semantic_names,
157 semantic_indexes,
158 &st_ctx->vert_shader);
159 }
160
161 /* fragment shader */
162 st_ctx->fs = util_make_fragment_passthrough_shader(st_ctx->pipe,
163 &st_ctx->frag_shader);
164
165 st_ctx->pipe->bind_fs_state(st_ctx->pipe, st_ctx->fs);
166 st_ctx->pipe->bind_vs_state(st_ctx->pipe, st_ctx->vs);
167
168 return st_ctx;
169 }