gallium: set pipe_context uploaders in drivers (v3)
[mesa.git] / src / gallium / drivers / i915 / i915_context.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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 "util/u_upload_mgr.h"
41 #include "pipe/p_screen.h"
42
43
44 DEBUG_GET_ONCE_BOOL_OPTION(i915_no_vbuf, "I915_NO_VBUF", FALSE)
45
46
47 /*
48 * Draw functions
49 */
50
51
52 static void
53 i915_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
54 {
55 struct i915_context *i915 = i915_context(pipe);
56 struct draw_context *draw = i915->draw;
57 const void *mapped_indices = NULL;
58 unsigned i;
59
60 /*
61 * Ack vs contants here, helps ipers a lot.
62 */
63 i915->dirty &= ~I915_NEW_VS_CONSTANTS;
64
65 if (i915->dirty)
66 i915_update_derived(i915);
67
68 /*
69 * Map vertex buffers
70 */
71 for (i = 0; i < i915->nr_vertex_buffers; i++) {
72 const void *buf = i915->vertex_buffers[i].user_buffer;
73 if (!buf)
74 buf = i915_buffer(i915->vertex_buffers[i].buffer)->data;
75 draw_set_mapped_vertex_buffer(draw, i, buf, ~0);
76 }
77
78 /*
79 * Map index buffer, if present
80 */
81 if (info->indexed) {
82 mapped_indices = i915->index_buffer.user_buffer;
83 if (!mapped_indices)
84 mapped_indices = i915_buffer(i915->index_buffer.buffer)->data;
85 draw_set_indexes(draw,
86 (ubyte *) mapped_indices + i915->index_buffer.offset,
87 i915->index_buffer.index_size, ~0);
88 }
89
90 if (i915->constants[PIPE_SHADER_VERTEX])
91 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0,
92 i915_buffer(i915->constants[PIPE_SHADER_VERTEX])->data,
93 (i915->current.num_user_constants[PIPE_SHADER_VERTEX] *
94 4 * sizeof(float)));
95 else
96 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0, NULL, 0);
97
98 if (i915->num_vertex_sampler_views > 0)
99 i915_prepare_vertex_sampling(i915);
100
101 /*
102 * Do the drawing
103 */
104 draw_vbo(i915->draw, info);
105
106 /*
107 * unmap vertex/index buffers
108 */
109 for (i = 0; i < i915->nr_vertex_buffers; i++) {
110 draw_set_mapped_vertex_buffer(i915->draw, i, NULL, 0);
111 }
112 if (mapped_indices)
113 draw_set_indexes(draw, NULL, 0, 0);
114
115 if (i915->num_vertex_sampler_views > 0)
116 i915_cleanup_vertex_sampling(i915);
117
118 /*
119 * Instead of flushing on every state change, we flush once here
120 * when we fire the vbo.
121 */
122 draw_flush(i915->draw);
123 }
124
125
126 /*
127 * Generic context functions
128 */
129
130
131 static void i915_destroy(struct pipe_context *pipe)
132 {
133 struct i915_context *i915 = i915_context(pipe);
134 int i;
135
136 if (i915->blitter)
137 util_blitter_destroy(i915->blitter);
138
139 draw_destroy(i915->draw);
140
141 if (i915->base.stream_uploader)
142 u_upload_destroy(i915->base.stream_uploader);
143
144 if(i915->batch)
145 i915->iws->batchbuffer_destroy(i915->batch);
146
147 /* unbind framebuffer */
148 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
149 pipe_surface_reference(&i915->framebuffer.cbufs[i], NULL);
150 }
151 pipe_surface_reference(&i915->framebuffer.zsbuf, NULL);
152
153 /* unbind constant buffers */
154 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
155 pipe_resource_reference(&i915->constants[i], NULL);
156 }
157
158 FREE(i915);
159 }
160
161 struct pipe_context *
162 i915_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
163 {
164 struct i915_context *i915;
165
166 i915 = CALLOC_STRUCT(i915_context);
167 if (!i915)
168 return NULL;
169
170 i915->iws = i915_screen(screen)->iws;
171 i915->base.screen = screen;
172 i915->base.priv = priv;
173 i915->base.stream_uploader = u_upload_create_default(&i915->base);
174 i915->base.const_uploader = i915->base.stream_uploader;
175
176 i915->base.destroy = i915_destroy;
177
178 if (i915_screen(screen)->debug.use_blitter)
179 i915->base.clear = i915_clear_blitter;
180 else
181 i915->base.clear = i915_clear_render;
182
183 i915->base.draw_vbo = i915_draw_vbo;
184
185 /* init this before draw */
186 slab_create(&i915->transfer_pool, sizeof(struct pipe_transfer),
187 16);
188 slab_create(&i915->texture_transfer_pool, sizeof(struct i915_transfer),
189 16);
190
191 /* Batch stream debugging is a bit hacked up at the moment:
192 */
193 i915->batch = i915->iws->batchbuffer_create(i915->iws);
194
195 /*
196 * Create drawing context and plug our rendering stage into it.
197 */
198 i915->draw = draw_create(&i915->base);
199 assert(i915->draw);
200 if (!debug_get_option_i915_no_vbuf()) {
201 draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));
202 } else {
203 draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
204 }
205
206 i915_init_surface_functions(i915);
207 i915_init_state_functions(i915);
208 i915_init_flush_functions(i915);
209 i915_init_resource_functions(i915);
210 i915_init_query_functions(i915);
211
212 /* Create blitter. */
213 i915->blitter = util_blitter_create(&i915->base);
214 assert(i915->blitter);
215
216 /* must be done before installing Draw stages */
217 util_blitter_cache_all_shaders(i915->blitter);
218
219 draw_install_aaline_stage(i915->draw, &i915->base);
220 draw_install_aapoint_stage(i915->draw, &i915->base);
221 draw_enable_point_sprites(i915->draw, TRUE);
222
223 i915->dirty = ~0;
224 i915->hardware_dirty = ~0;
225 i915->immediate_dirty = ~0;
226 i915->dynamic_dirty = ~0;
227 i915->static_dirty = ~0;
228 i915->flush_dirty = 0;
229
230 return &i915->base;
231 }