i915g: Cleanup the vertex sampler interface a bit.
[mesa.git] / src / gallium / drivers / i915 / i915_context.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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 #include "i915_context.h"
29 #include "i915_state.h"
30 #include "i915_screen.h"
31 #include "i915_surface.h"
32 #include "i915_query.h"
33 #include "i915_batch.h"
34 #include "i915_resource.h"
35
36 #include "draw/draw_context.h"
37 #include "pipe/p_defines.h"
38 #include "util/u_inlines.h"
39 #include "util/u_memory.h"
40 #include "pipe/p_screen.h"
41
42
43 DEBUG_GET_ONCE_BOOL_OPTION(i915_no_vbuf, "I915_NO_VBUF", FALSE)
44
45
46 /*
47 * Draw functions
48 */
49
50
51 static void
52 i915_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
53 {
54 struct i915_context *i915 = i915_context(pipe);
55 struct draw_context *draw = i915->draw;
56 void *mapped_indices = NULL;
57
58
59 /*
60 * Ack vs contants here, helps ipers a lot.
61 */
62 i915->dirty &= ~I915_NEW_VS_CONSTANTS;
63
64 if (i915->dirty)
65 i915_update_derived(i915);
66
67 /*
68 * Map index buffer, if present
69 */
70 if (info->indexed && i915->index_buffer.buffer)
71 mapped_indices = i915_buffer(i915->index_buffer.buffer)->data;
72 draw_set_mapped_index_buffer(draw, mapped_indices);
73
74 if (i915->constants[PIPE_SHADER_VERTEX])
75 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0,
76 i915_buffer(i915->constants[PIPE_SHADER_VERTEX])->data,
77 (i915->current.num_user_constants[PIPE_SHADER_VERTEX] *
78 4 * sizeof(float)));
79 else
80 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0, NULL, 0);
81
82 if (i915->num_vertex_sampler_views > 0)
83 i915_prepare_vertex_sampling(i915);
84
85 /*
86 * Do the drawing
87 */
88 draw_vbo(i915->draw, info);
89
90 if (mapped_indices)
91 draw_set_mapped_index_buffer(draw, NULL);
92
93 if (i915->num_vertex_sampler_views > 0)
94 i915_cleanup_vertex_sampling(i915);
95 }
96
97
98 /*
99 * Generic context functions
100 */
101
102
103 static void i915_destroy(struct pipe_context *pipe)
104 {
105 struct i915_context *i915 = i915_context(pipe);
106 int i;
107
108 if (i915->blitter)
109 util_blitter_destroy(i915->blitter);
110
111 draw_destroy(i915->draw);
112
113 if(i915->batch)
114 i915->iws->batchbuffer_destroy(i915->batch);
115
116 /* unbind framebuffer */
117 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
118 pipe_surface_reference(&i915->framebuffer.cbufs[i], NULL);
119 }
120 pipe_surface_reference(&i915->framebuffer.zsbuf, NULL);
121
122 /* unbind constant buffers */
123 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
124 pipe_resource_reference(&i915->constants[i], NULL);
125 }
126
127 FREE(i915);
128 }
129
130 struct pipe_context *
131 i915_create_context(struct pipe_screen *screen, void *priv)
132 {
133 struct i915_context *i915;
134
135 i915 = CALLOC_STRUCT(i915_context);
136 if (i915 == NULL)
137 return NULL;
138
139 i915->iws = i915_screen(screen)->iws;
140 i915->base.winsys = NULL;
141 i915->base.screen = screen;
142 i915->base.priv = priv;
143
144 i915->base.destroy = i915_destroy;
145
146 if (i915_screen(screen)->debug.use_blitter)
147 i915->base.clear = i915_clear_blitter;
148 else
149 i915->base.clear = i915_clear_render;
150
151 i915->base.draw_vbo = i915_draw_vbo;
152
153 /* init this before draw */
154 util_slab_create(&i915->transfer_pool, sizeof(struct pipe_transfer),
155 16, UTIL_SLAB_SINGLETHREADED);
156 util_slab_create(&i915->texture_transfer_pool, sizeof(struct i915_transfer),
157 16, UTIL_SLAB_SINGLETHREADED);
158
159 /* Batch stream debugging is a bit hacked up at the moment:
160 */
161 i915->batch = i915->iws->batchbuffer_create(i915->iws);
162
163 /*
164 * Create drawing context and plug our rendering stage into it.
165 */
166 i915->draw = draw_create(&i915->base);
167 assert(i915->draw);
168 if (!debug_get_option_i915_no_vbuf()) {
169 draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));
170 } else {
171 draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
172 }
173
174 i915_init_surface_functions(i915);
175 i915_init_state_functions(i915);
176 i915_init_flush_functions(i915);
177 i915_init_resource_functions(i915);
178 i915_init_query_functions(i915);
179
180 draw_install_aaline_stage(i915->draw, &i915->base);
181 draw_install_aapoint_stage(i915->draw, &i915->base);
182 draw_enable_point_sprites(i915->draw, TRUE);
183
184 /* augmented draw pipeline clobbers state functions */
185 i915_init_fixup_state_functions(i915);
186
187 /* Create blitter last - calls state creation functions. */
188 i915->blitter = util_blitter_create(&i915->base);
189 assert(i915->blitter);
190
191 i915->dirty = ~0;
192 i915->hardware_dirty = ~0;
193 i915->immediate_dirty = ~0;
194 i915->dynamic_dirty = ~0;
195 i915->static_dirty = ~0;
196 i915->flush_dirty = 0;
197
198 return &i915->base;
199 }