Merge branch 'remove-intel-dri1'
[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_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/internal/p_winsys_screen.h"
38 #include "pipe/p_inlines.h"
39 #include "util/u_memory.h"
40 #include "pipe/p_screen.h"
41
42
43 /*
44 * Draw functions
45 */
46
47
48 static void
49 i915_draw_range_elements(struct pipe_context *pipe,
50 struct pipe_buffer *indexBuffer,
51 unsigned indexSize,
52 unsigned min_index,
53 unsigned max_index,
54 unsigned prim, unsigned start, unsigned count)
55 {
56 struct i915_context *i915 = i915_context(pipe);
57 struct draw_context *draw = i915->draw;
58 unsigned i;
59
60 if (i915->dirty)
61 i915_update_derived(i915);
62
63 /*
64 * Map vertex buffers
65 */
66 for (i = 0; i < i915->num_vertex_buffers; i++) {
67 void *buf = pipe_buffer_map(pipe->screen, i915->vertex_buffer[i].buffer,
68 PIPE_BUFFER_USAGE_CPU_READ);
69 draw_set_mapped_vertex_buffer(draw, i, buf);
70 }
71
72 /*
73 * Map index buffer, if present
74 */
75 if (indexBuffer) {
76 void *mapped_indexes = pipe_buffer_map(pipe->screen, indexBuffer,
77 PIPE_BUFFER_USAGE_CPU_READ);
78 draw_set_mapped_element_buffer_range(draw, indexSize,
79 min_index,
80 max_index,
81 mapped_indexes);
82 } else {
83 draw_set_mapped_element_buffer(draw, 0, NULL);
84 }
85
86
87 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX,
88 i915->current.constants[PIPE_SHADER_VERTEX],
89 (i915->current.num_user_constants[PIPE_SHADER_VERTEX] *
90 4 * sizeof(float)));
91
92 /*
93 * Do the drawing
94 */
95 draw_arrays(i915->draw, prim, start, count);
96
97 /*
98 * unmap vertex/index buffers
99 */
100 for (i = 0; i < i915->num_vertex_buffers; i++) {
101 pipe_buffer_unmap(pipe->screen, i915->vertex_buffer[i].buffer);
102 draw_set_mapped_vertex_buffer(draw, i, NULL);
103 }
104
105 if (indexBuffer) {
106 pipe_buffer_unmap(pipe->screen, indexBuffer);
107 draw_set_mapped_element_buffer_range(draw, 0, start, start + count - 1, NULL);
108 }
109 }
110
111 static void
112 i915_draw_elements(struct pipe_context *pipe,
113 struct pipe_buffer *indexBuffer,
114 unsigned indexSize,
115 unsigned prim, unsigned start, unsigned count)
116 {
117 i915_draw_range_elements(pipe, indexBuffer,
118 indexSize,
119 0, 0xffffffff,
120 prim, start, count);
121 }
122
123 static void
124 i915_draw_arrays(struct pipe_context *pipe,
125 unsigned prim, unsigned start, unsigned count)
126 {
127 i915_draw_elements(pipe, NULL, 0, prim, start, count);
128 }
129
130
131 /*
132 * Is referenced functions
133 */
134
135
136 static unsigned int
137 i915_is_texture_referenced(struct pipe_context *pipe,
138 struct pipe_texture *texture,
139 unsigned face, unsigned level)
140 {
141 /**
142 * FIXME: Return the corrent result. We can't alays return referenced
143 * since it causes a double flush within the vbo module.
144 */
145 #if 0
146 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
147 #else
148 return 0;
149 #endif
150 }
151
152 static unsigned int
153 i915_is_buffer_referenced(struct pipe_context *pipe,
154 struct pipe_buffer *buf)
155 {
156 /*
157 * Since we never expose hardware buffers to the state tracker
158 * they can never be referenced, so this isn't a lie
159 */
160 return 0;
161 }
162
163
164 /*
165 * Generic context functions
166 */
167
168
169 static void i915_destroy(struct pipe_context *pipe)
170 {
171 struct i915_context *i915 = i915_context(pipe);
172 int i;
173
174 draw_destroy(i915->draw);
175
176 if(i915->batch)
177 i915->iws->batchbuffer_destroy(i915->batch);
178
179 /* unbind framebuffer */
180 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
181 pipe_surface_reference(&i915->framebuffer.cbufs[i], NULL);
182 }
183 pipe_surface_reference(&i915->framebuffer.zsbuf, NULL);
184
185 FREE(i915);
186 }
187
188 struct pipe_context *
189 i915_create_context(struct pipe_screen *screen)
190 {
191 struct i915_context *i915;
192
193 i915 = CALLOC_STRUCT(i915_context);
194 if (i915 == NULL)
195 return NULL;
196
197 i915->iws = i915_screen(screen)->iws;
198 i915->base.winsys = NULL;
199 i915->base.screen = screen;
200
201 i915->base.destroy = i915_destroy;
202
203 i915->base.clear = i915_clear;
204
205 i915->base.draw_arrays = i915_draw_arrays;
206 i915->base.draw_elements = i915_draw_elements;
207 i915->base.draw_range_elements = i915_draw_range_elements;
208
209 i915->base.is_texture_referenced = i915_is_texture_referenced;
210 i915->base.is_buffer_referenced = i915_is_buffer_referenced;
211
212 /*
213 * Create drawing context and plug our rendering stage into it.
214 */
215 i915->draw = draw_create();
216 assert(i915->draw);
217 if (!debug_get_bool_option("I915_NO_VBUF", FALSE)) {
218 draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));
219 } else {
220 draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
221 }
222
223 i915_init_surface_functions(i915);
224 i915_init_state_functions(i915);
225 i915_init_flush_functions(i915);
226
227 draw_install_aaline_stage(i915->draw, &i915->base);
228 draw_install_aapoint_stage(i915->draw, &i915->base);
229
230 i915->dirty = ~0;
231 i915->hardware_dirty = ~0;
232
233 /* Batch stream debugging is a bit hacked up at the moment:
234 */
235 i915->batch = i915->iws->batchbuffer_create(i915->iws);
236
237 return &i915->base;
238 }