OSDN Git Service

tgsi: Simplify implementation of few interpreter's instructions.
[android-x86/external-mesa.git] / src / gallium / drivers / softpipe / sp_draw_arrays.c
1 /**************************************************************************
2  * 
3  * Copyright 2007 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 /* Author:
29  *    Brian Paul
30  *    Keith Whitwell
31  */
32
33
34 #include "pipe/p_defines.h"
35 #include "pipe/p_context.h"
36 #include "pipe/internal/p_winsys_screen.h"
37 #include "pipe/p_inlines.h"
38 #include "util/u_prim.h"
39
40 #include "sp_context.h"
41 #include "sp_query.h"
42 #include "sp_state.h"
43
44 #include "draw/draw_context.h"
45
46
47
48 static void
49 softpipe_map_constant_buffers(struct softpipe_context *sp)
50 {
51    struct pipe_winsys *ws = sp->pipe.winsys;
52    uint i, vssize, gssize;
53
54    for (i = 0; i < PIPE_SHADER_TYPES; i++) {
55       if (sp->constants[i].buffer && sp->constants[i].buffer->size)
56          sp->mapped_constants[i] = ws->buffer_map(ws, sp->constants[i].buffer,
57                                                   PIPE_BUFFER_USAGE_CPU_READ);
58    }
59
60    if (sp->constants[PIPE_SHADER_VERTEX].buffer)
61       vssize = sp->constants[PIPE_SHADER_VERTEX].buffer->size;
62    else
63       vssize = 0;
64
65    if (sp->constants[PIPE_SHADER_GEOMETRY].buffer)
66       gssize = sp->constants[PIPE_SHADER_GEOMETRY].buffer->size;
67    else
68       gssize = 0;
69
70    draw_set_mapped_constant_buffer(sp->draw, PIPE_SHADER_VERTEX,
71                                    sp->mapped_constants[PIPE_SHADER_VERTEX],
72                                    vssize);
73    draw_set_mapped_constant_buffer(sp->draw, PIPE_SHADER_GEOMETRY,
74                                    sp->mapped_constants[PIPE_SHADER_GEOMETRY],
75                                    gssize);
76 }
77
78
79 static void
80 softpipe_unmap_constant_buffers(struct softpipe_context *sp)
81 {
82    struct pipe_winsys *ws = sp->pipe.winsys;
83    uint i;
84
85    /* really need to flush all prims since the vert/frag shaders const buffers
86     * are going away now.
87     */
88    draw_flush(sp->draw);
89
90    draw_set_mapped_constant_buffer(sp->draw, PIPE_SHADER_VERTEX, NULL, 0);
91    draw_set_mapped_constant_buffer(sp->draw, PIPE_SHADER_GEOMETRY, NULL, 0);
92
93    for (i = 0; i < PIPE_SHADER_TYPES; i++) {
94       if (sp->constants[i].buffer && sp->constants[i].buffer->size)
95          ws->buffer_unmap(ws, sp->constants[i].buffer);
96       sp->mapped_constants[i] = NULL;
97    }
98 }
99
100
101 boolean
102 softpipe_draw_arrays(struct pipe_context *pipe, unsigned mode,
103                      unsigned start, unsigned count)
104 {
105    return softpipe_draw_elements(pipe, NULL, 0, mode, start, count);
106 }
107
108
109 /**
110  * Draw vertex arrays, with optional indexing.
111  * Basically, map the vertex buffers (and drawing surfaces), then hand off
112  * the drawing to the 'draw' module.
113  */
114 boolean
115 softpipe_draw_range_elements(struct pipe_context *pipe,
116                              struct pipe_buffer *indexBuffer,
117                              unsigned indexSize,
118                              unsigned min_index,
119                              unsigned max_index,
120                              unsigned mode, unsigned start, unsigned count)
121 {
122    struct softpipe_context *sp = softpipe_context(pipe);
123    struct draw_context *draw = sp->draw;
124    unsigned i;
125
126    if (!softpipe_check_render_cond(sp))
127       return TRUE;
128
129    sp->reduced_api_prim = u_reduced_prim(mode);
130
131    if (sp->dirty)
132       softpipe_update_derived( sp );
133
134    softpipe_map_transfers(sp);
135    softpipe_map_constant_buffers(sp);
136
137    /*
138     * Map vertex buffers
139     */
140    for (i = 0; i < sp->num_vertex_buffers; i++) {
141       void *buf
142          = pipe_buffer_map(pipe->screen,
143                                     sp->vertex_buffer[i].buffer,
144                                     PIPE_BUFFER_USAGE_CPU_READ);
145       draw_set_mapped_vertex_buffer(draw, i, buf);
146    }
147
148    /* Map index buffer, if present */
149    if (indexBuffer) {
150       void *mapped_indexes
151          = pipe_buffer_map(pipe->screen, indexBuffer,
152                                     PIPE_BUFFER_USAGE_CPU_READ);
153       draw_set_mapped_element_buffer_range(draw, indexSize,
154                                            min_index,
155                                            max_index,
156                                            mapped_indexes);
157    }
158    else {
159       /* no index/element buffer */
160       draw_set_mapped_element_buffer_range(draw, 0, start,
161                                            start + count - 1, NULL);
162    }
163
164    /* draw! */
165    draw_arrays(draw, mode, start, count);
166
167    /*
168     * unmap vertex/index buffers - will cause draw module to flush
169     */
170    for (i = 0; i < sp->num_vertex_buffers; i++) {
171       draw_set_mapped_vertex_buffer(draw, i, NULL);
172       pipe_buffer_unmap(pipe->screen, sp->vertex_buffer[i].buffer);
173    }
174    if (indexBuffer) {
175       draw_set_mapped_element_buffer(draw, 0, NULL);
176       pipe_buffer_unmap(pipe->screen, indexBuffer);
177    }
178
179
180    /* Note: leave drawing surfaces mapped */
181    softpipe_unmap_constant_buffers(sp);
182
183    sp->dirty_render_cache = TRUE;
184    
185    return TRUE;
186 }
187
188
189 boolean
190 softpipe_draw_elements(struct pipe_context *pipe,
191                        struct pipe_buffer *indexBuffer,
192                        unsigned indexSize,
193                        unsigned mode, unsigned start, unsigned count)
194 {
195    return softpipe_draw_range_elements( pipe, indexBuffer,
196                                         indexSize,
197                                         0, 0xffffffff,
198                                         mode, start, count );
199 }