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_batch.h"
30 #include "i915_debug.h"
31 #include "i915_query.h"
32 #include "i915_resource.h"
33 #include "i915_screen.h"
34 #include "i915_state.h"
35 #include "i915_surface.h"
36 
37 #include "draw/draw_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_screen.h"
40 #include "util/u_draw.h"
41 #include "util/u_inlines.h"
42 #include "util/u_memory.h"
43 #include "util/u_prim.h"
44 #include "util/u_upload_mgr.h"
45 
46 /*
47  * Draw functions
48  */
49 
50 static void
i915_draw_vbo(struct pipe_context * pipe,const struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)51 i915_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info,
52               unsigned drawid_offset,
53               const struct pipe_draw_indirect_info *indirect,
54               const struct pipe_draw_start_count_bias *draws,
55               unsigned num_draws)
56 {
57    if (num_draws > 1) {
58       util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);
59       return;
60    }
61 
62    struct i915_context *i915 = i915_context(pipe);
63    struct draw_context *draw = i915->draw;
64    const void *mapped_indices = NULL;
65    unsigned i;
66 
67    if (!u_trim_pipe_prim(info->mode, (unsigned *)&draws[0].count))
68       return;
69 
70    /*
71     * Ack vs contants here, helps ipers a lot.
72     */
73    i915->dirty &= ~I915_NEW_VS_CONSTANTS;
74 
75    if (i915->dirty)
76       i915_update_derived(i915);
77 
78    /*
79     * Map vertex buffers
80     */
81    for (i = 0; i < i915->nr_vertex_buffers; i++) {
82       const void *buf = i915->vertex_buffers[i].is_user_buffer
83                            ? i915->vertex_buffers[i].buffer.user
84                            : NULL;
85       if (!buf) {
86          if (!i915->vertex_buffers[i].buffer.resource)
87             continue;
88          buf = i915_buffer(i915->vertex_buffers[i].buffer.resource)->data;
89       }
90       draw_set_mapped_vertex_buffer(draw, i, buf, ~0);
91    }
92 
93    /*
94     * Map index buffer, if present
95     */
96    if (info->index_size) {
97       mapped_indices = info->has_user_indices ? info->index.user : NULL;
98       if (!mapped_indices)
99          mapped_indices = i915_buffer(info->index.resource)->data;
100       draw_set_indexes(draw, (ubyte *)mapped_indices, info->index_size, ~0);
101    }
102 
103    if (i915->constants[PIPE_SHADER_VERTEX])
104       draw_set_mapped_constant_buffer(
105          draw, PIPE_SHADER_VERTEX, 0,
106          i915_buffer(i915->constants[PIPE_SHADER_VERTEX])->data,
107          (i915->current.num_user_constants[PIPE_SHADER_VERTEX] * 4 *
108           sizeof(float)));
109    else
110       draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0, NULL, 0);
111 
112    /*
113     * Do the drawing
114     */
115    draw_vbo(i915->draw, info, drawid_offset, NULL, draws, num_draws, 0);
116 
117    /*
118     * unmap vertex/index buffers
119     */
120    for (i = 0; i < i915->nr_vertex_buffers; i++) {
121       draw_set_mapped_vertex_buffer(i915->draw, i, NULL, 0);
122    }
123    if (mapped_indices)
124       draw_set_indexes(draw, NULL, 0, 0);
125 
126    /*
127     * Instead of flushing on every state change, we flush once here
128     * when we fire the vbo.
129     */
130    draw_flush(i915->draw);
131 }
132 
133 /*
134  * Generic context functions
135  */
136 
137 static void
i915_destroy(struct pipe_context * pipe)138 i915_destroy(struct pipe_context *pipe)
139 {
140    struct i915_context *i915 = i915_context(pipe);
141    int i;
142 
143    if (i915->blitter)
144       util_blitter_destroy(i915->blitter);
145 
146    draw_destroy(i915->draw);
147 
148    if (i915->base.stream_uploader)
149       u_upload_destroy(i915->base.stream_uploader);
150 
151    if (i915->batch)
152       i915->iws->batchbuffer_destroy(i915->batch);
153 
154    /* unbind framebuffer */
155    for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
156       pipe_surface_reference(&i915->framebuffer.cbufs[i], NULL);
157    }
158    pipe_surface_reference(&i915->framebuffer.zsbuf, NULL);
159 
160    /* unbind constant buffers */
161    for (i = 0; i < PIPE_SHADER_TYPES; i++) {
162       pipe_resource_reference(&i915->constants[i], NULL);
163    }
164 
165    FREE(i915);
166 }
167 
168 static void
i915_set_debug_callback(struct pipe_context * pipe,const struct pipe_debug_callback * cb)169 i915_set_debug_callback(struct pipe_context *pipe,
170                         const struct pipe_debug_callback *cb)
171 {
172    struct i915_context *i915 = i915_context(pipe);
173 
174    if (cb)
175       i915->debug = *cb;
176    else
177       memset(&i915->debug, 0, sizeof(i915->debug));
178 }
179 
180 struct pipe_context *
i915_create_context(struct pipe_screen * screen,void * priv,unsigned flags)181 i915_create_context(struct pipe_screen *screen, void *priv, unsigned flags)
182 {
183    struct i915_context *i915;
184 
185    i915 = CALLOC_STRUCT(i915_context);
186    if (!i915)
187       return NULL;
188 
189    i915->iws = i915_screen(screen)->iws;
190    i915->base.screen = screen;
191    i915->base.priv = priv;
192    i915->base.stream_uploader = u_upload_create_default(&i915->base);
193    i915->base.const_uploader = i915->base.stream_uploader;
194    i915->base.set_debug_callback = i915_set_debug_callback;
195 
196    i915->base.destroy = i915_destroy;
197 
198    if (i915_screen(screen)->debug.use_blitter)
199       i915->base.clear = i915_clear_blitter;
200    else
201       i915->base.clear = i915_clear_render;
202 
203    i915->base.draw_vbo = i915_draw_vbo;
204 
205    /* init this before draw */
206    slab_create(&i915->transfer_pool, sizeof(struct pipe_transfer), 16);
207    slab_create(&i915->texture_transfer_pool, sizeof(struct i915_transfer), 16);
208 
209    /* Batch stream debugging is a bit hacked up at the moment:
210     */
211    i915->batch = i915->iws->batchbuffer_create(i915->iws);
212 
213    /*
214     * Create drawing context and plug our rendering stage into it.
215     */
216    i915->draw = draw_create(&i915->base);
217    assert(i915->draw);
218    if (i915_debug & DBG_VBUF) {
219       draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));
220    } else {
221       draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));
222    }
223 
224    i915_init_surface_functions(i915);
225    i915_init_state_functions(i915);
226    i915_init_flush_functions(i915);
227    i915_init_resource_functions(i915);
228    i915_init_query_functions(i915);
229 
230    /* Create blitter. */
231    i915->blitter = util_blitter_create(&i915->base);
232    assert(i915->blitter);
233 
234    /* must be done before installing Draw stages */
235    i915->no_log_program_errors = true;
236    util_blitter_cache_all_shaders(i915->blitter);
237    i915->no_log_program_errors = false;
238 
239    draw_install_aaline_stage(i915->draw, &i915->base);
240    draw_install_aapoint_stage(i915->draw, &i915->base);
241    draw_enable_point_sprites(i915->draw, true);
242 
243    i915->dirty = ~0;
244    i915->hardware_dirty = ~0;
245    i915->immediate_dirty = ~0;
246    i915->dynamic_dirty = ~0;
247    i915->static_dirty = ~0;
248    i915->flush_dirty = 0;
249 
250    i915->current.fixup_swizzle = ~0;
251 
252    return &i915->base;
253 }
254