Merge branch '7.8'
[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_batch.h"
33 #include "i915_resource.h"
34
35 #include "draw/draw_context.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_inlines.h"
38 #include "util/u_memory.h"
39 #include "pipe/p_screen.h"
40
41
42 /*
43 * Draw functions
44 */
45
46
47 static void
48 i915_draw_range_elements(struct pipe_context *pipe,
49 struct pipe_resource *indexBuffer,
50 unsigned indexSize,
51 int indexBias,
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 = i915_buffer(i915->vertex_buffer[i].buffer)->data;
68 draw_set_mapped_vertex_buffer(draw, i, buf);
69 }
70
71 /*
72 * Map index buffer, if present
73 */
74 if (indexBuffer) {
75 void *mapped_indexes = i915_buffer(indexBuffer)->data;
76 draw_set_mapped_element_buffer_range(draw, indexSize, indexBias,
77 min_index,
78 max_index,
79 mapped_indexes);
80 } else {
81 draw_set_mapped_element_buffer(draw, 0, 0, NULL);
82 }
83
84
85 draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0,
86 i915->current.constants[PIPE_SHADER_VERTEX],
87 (i915->current.num_user_constants[PIPE_SHADER_VERTEX] *
88 4 * sizeof(float)));
89
90 /*
91 * Do the drawing
92 */
93 draw_arrays(i915->draw, prim, start, count);
94
95 /*
96 * unmap vertex/index buffers
97 */
98 for (i = 0; i < i915->num_vertex_buffers; i++) {
99 draw_set_mapped_vertex_buffer(draw, i, NULL);
100 }
101
102 if (indexBuffer) {
103 draw_set_mapped_element_buffer(draw, 0, 0, NULL);
104 }
105 }
106
107 static void
108 i915_draw_elements(struct pipe_context *pipe,
109 struct pipe_resource *indexBuffer,
110 unsigned indexSize, int indexBias,
111 unsigned prim, unsigned start, unsigned count)
112 {
113 i915_draw_range_elements(pipe, indexBuffer,
114 indexSize, indexBias,
115 0, 0xffffffff,
116 prim, start, count);
117 }
118
119 static void
120 i915_draw_arrays(struct pipe_context *pipe,
121 unsigned prim, unsigned start, unsigned count)
122 {
123 i915_draw_elements(pipe, NULL, 0, 0, prim, start, count);
124 }
125
126
127
128
129 /*
130 * Generic context functions
131 */
132
133
134 static void i915_destroy(struct pipe_context *pipe)
135 {
136 struct i915_context *i915 = i915_context(pipe);
137 int i;
138
139 draw_destroy(i915->draw);
140
141 if(i915->batch)
142 i915->iws->batchbuffer_destroy(i915->batch);
143
144 /* unbind framebuffer */
145 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
146 pipe_surface_reference(&i915->framebuffer.cbufs[i], NULL);
147 }
148 pipe_surface_reference(&i915->framebuffer.zsbuf, NULL);
149
150 FREE(i915);
151 }
152
153 struct pipe_context *
154 i915_create_context(struct pipe_screen *screen, void *priv)
155 {
156 struct i915_context *i915;
157
158 i915 = CALLOC_STRUCT(i915_context);
159 if (i915 == NULL)
160 return NULL;
161
162 i915->iws = i915_screen(screen)->iws;
163 i915->base.winsys = NULL;
164 i915->base.screen = screen;
165 i915->base.priv = priv;
166
167 i915->base.destroy = i915_destroy;
168
169 i915->base.clear = i915_clear;
170
171 i915->base.draw_arrays = i915_draw_arrays;
172 i915->base.draw_elements = i915_draw_elements;
173 i915->base.draw_range_elements = i915_draw_range_elements;
174
175 /*
176 * Create drawing context and plug our rendering stage into it.
177 */
178 i915->draw = draw_create(&i915->base);
179 assert(i915->draw);
180 if (!debug_get_bool_option("I915_NO_VBUF", FALSE)) {
181 draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));
182 } else {
183 draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
184 }
185
186 i915_init_surface_functions(i915);
187 i915_init_state_functions(i915);
188 i915_init_flush_functions(i915);
189 i915_init_resource_functions(i915);
190
191 draw_install_aaline_stage(i915->draw, &i915->base);
192 draw_install_aapoint_stage(i915->draw, &i915->base);
193
194 i915->dirty = ~0;
195 i915->hardware_dirty = ~0;
196
197 /* Batch stream debugging is a bit hacked up at the moment:
198 */
199 i915->batch = i915->iws->batchbuffer_create(i915->iws);
200
201 return &i915->base;
202 }