i915: Implement and use the reworked batchbuffer code
[mesa.git] / src / gallium / drivers / i915simple / 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_winsys.h"
30 #include "i915_state.h"
31 #include "i915_batch.h"
32 #include "i915_texture.h"
33 #include "i915_reg.h"
34
35 #include "draw/draw_context.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_winsys.h"
38 #include "pipe/p_util.h"
39 #include "pipe/p_screen.h"
40
41
42 static void i915_destroy( struct pipe_context *pipe )
43 {
44 struct i915_context *i915 = i915_context( pipe );
45
46 draw_destroy( i915->draw );
47
48 FREE( i915 );
49 }
50
51
52 static boolean
53 i915_draw_range_elements(struct pipe_context *pipe,
54 struct pipe_buffer *indexBuffer,
55 unsigned indexSize,
56 unsigned min_index,
57 unsigned max_index,
58 unsigned prim, unsigned start, unsigned count)
59 {
60 struct i915_context *i915 = i915_context( pipe );
61 struct draw_context *draw = i915->draw;
62 unsigned i;
63
64 if (i915->dirty)
65 i915_update_derived( i915 );
66
67 /*
68 * Map vertex buffers
69 */
70 for (i = 0; i < i915->num_vertex_buffers; i++) {
71 void *buf
72 = pipe->winsys->buffer_map(pipe->winsys,
73 i915->vertex_buffer[i].buffer,
74 PIPE_BUFFER_USAGE_CPU_READ);
75 draw_set_mapped_vertex_buffer(draw, i, buf);
76 }
77 /* Map index buffer, if present */
78 if (indexBuffer) {
79 void *mapped_indexes
80 = pipe->winsys->buffer_map(pipe->winsys, indexBuffer,
81 PIPE_BUFFER_USAGE_CPU_READ);
82 draw_set_mapped_element_buffer_range(draw, indexSize,
83 min_index,
84 max_index,
85 mapped_indexes);
86 }
87 else {
88 /* no index/element buffer */
89 draw_set_mapped_element_buffer(draw, 0, NULL);
90 }
91
92
93 draw_set_mapped_constant_buffer(draw,
94 i915->current.constants[PIPE_SHADER_VERTEX],
95 ( i915->current.num_user_constants[PIPE_SHADER_VERTEX] *
96 4 * sizeof(float) ));
97
98 /* draw! */
99 draw_arrays(i915->draw, prim, start, count);
100
101 /*
102 * unmap vertex/index buffers
103 */
104 for (i = 0; i < i915->num_vertex_buffers; i++) {
105 pipe->winsys->buffer_unmap(pipe->winsys, i915->vertex_buffer[i].buffer);
106 draw_set_mapped_vertex_buffer(draw, i, NULL);
107 }
108 if (indexBuffer) {
109 pipe->winsys->buffer_unmap(pipe->winsys, indexBuffer);
110 draw_set_mapped_element_buffer_range(draw, 0, start, start + count - 1, NULL);
111 }
112
113 return TRUE;
114 }
115
116 static boolean
117 i915_draw_elements( struct pipe_context *pipe,
118 struct pipe_buffer *indexBuffer,
119 unsigned indexSize,
120 unsigned prim, unsigned start, unsigned count)
121 {
122 return i915_draw_range_elements( pipe, indexBuffer,
123 indexSize,
124 0, 0xffffffff,
125 prim, start, count );
126 }
127
128 static boolean i915_draw_arrays( struct pipe_context *pipe,
129 unsigned prim, unsigned start, unsigned count)
130 {
131 return i915_draw_elements(pipe, NULL, 0, prim, start, count);
132 }
133
134
135
136 struct pipe_context *i915_create_context( struct pipe_screen *screen,
137 struct pipe_winsys *pipe_winsys,
138 struct i915_winsys *i915_winsys )
139 {
140 struct i915_context *i915;
141
142 i915 = CALLOC_STRUCT(i915_context);
143 if (i915 == NULL)
144 return NULL;
145
146 i915->winsys = i915_winsys;
147 i915->pipe.winsys = pipe_winsys;
148 i915->pipe.screen = screen;
149
150 i915->pipe.destroy = i915_destroy;
151
152 i915->pipe.clear = i915_clear;
153
154
155 i915->pipe.draw_arrays = i915_draw_arrays;
156 i915->pipe.draw_elements = i915_draw_elements;
157 i915->pipe.draw_range_elements = i915_draw_range_elements;
158
159 /*
160 * Create drawing context and plug our rendering stage into it.
161 */
162 i915->draw = draw_create();
163 assert(i915->draw);
164 if (!GETENV("I915_NO_VBUF")) {
165 draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));
166 }
167 else {
168 draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
169 }
170
171 i915_init_surface_functions(i915);
172 i915_init_state_functions(i915);
173 i915_init_flush_functions(i915);
174 i915_init_texture_functions(i915);
175
176 draw_install_aaline_stage(i915->draw, &i915->pipe);
177 draw_install_aapoint_stage(i915->draw, &i915->pipe);
178
179 i915->dirty = ~0;
180 i915->hardware_dirty = ~0;
181
182 /* Batch stream debugging is a bit hacked up at the moment:
183 */
184 i915->batch = i915_winsys->batch_get(i915_winsys);
185 i915->batch->winsys = i915_winsys;
186
187 return &i915->pipe;
188 }
189