summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/i965/brw_draw.c
blob: a2bed6256b7593465f776a2410c3d6577caac88b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**************************************************************************
 * 
 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/


#include "util/u_prim.h"
#include "util/u_upload_mgr.h"

#include "brw_draw.h"
#include "brw_defines.h"
#include "brw_context.h"
#include "brw_state.h"
#include "brw_debug.h"
#include "brw_screen.h"

#include "brw_batchbuffer.h"


static uint32_t prim_to_hw_prim[PIPE_PRIM_POLYGON+1] = {
   _3DPRIM_POINTLIST,
   _3DPRIM_LINELIST,
   _3DPRIM_LINELOOP,
   _3DPRIM_LINESTRIP,
   _3DPRIM_TRILIST,
   _3DPRIM_TRISTRIP,
   _3DPRIM_TRIFAN,
   _3DPRIM_QUADLIST,
   _3DPRIM_QUADSTRIP,
   _3DPRIM_POLYGON
};



/* When the primitive changes, set a state bit and re-validate.  Not
 * the nicest and would rather deal with this by having all the
 * programs be immune to the active primitive (ie. cope with all
 * possibilities).  That may not be realistic however.
 */
static int brw_set_prim(struct brw_context *brw, unsigned prim )
{

   if (BRW_DEBUG & DEBUG_PRIMS)
      debug_printf("PRIM: %s\n", u_prim_name(prim));
   
   if (prim != brw->primitive) {
      unsigned reduced_prim;

      brw->primitive = prim;
      brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;

      reduced_prim = u_reduced_prim(prim);
      if (reduced_prim != brw->reduced_primitive) {
	 brw->reduced_primitive = reduced_prim;
	 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
      }
   }

   return prim_to_hw_prim[prim];
}



static int brw_emit_prim(struct brw_context *brw,
			 unsigned start,
			 unsigned count,
			 boolean indexed,
			 uint32_t hw_prim)
{
   struct brw_3d_primitive prim_packet;
   int ret;

   prim_packet.header.opcode = CMD_3D_PRIM;
   prim_packet.header.length = sizeof(prim_packet)/4 - 2;
   prim_packet.header.pad = 0;
   prim_packet.header.topology = hw_prim;
   prim_packet.header.indexed = indexed;

   prim_packet.verts_per_instance = count;
   prim_packet.start_vert_location = start;
   if (indexed)
      prim_packet.start_vert_location += brw->ib.start_vertex_offset;
   prim_packet.instance_count = 1;
   prim_packet.start_instance_location = 0;
   prim_packet.base_vert_location = 0; // prim->basevertex; XXX: add this to gallium


   /* If we're set to always flush, do it before and after the primitive emit.
    * We want to catch both missed flushes that hurt instruction/state cache
    * and missed flushes of the render cache as it heads to other parts of
    * the besides the draw code.
    */
   if (0) {
      BEGIN_BATCH(1, IGNORE_CLIPRECTS);
      OUT_BATCH((CMD_MI_FLUSH << 16) | BRW_FLUSH_STATE_CACHE);
      ADVANCE_BATCH();
   }
   if (prim_packet.verts_per_instance) {
      ret = brw_batchbuffer_data( brw->batch, &prim_packet,
				  sizeof(prim_packet), LOOP_CLIPRECTS);
      if (ret)
	 return ret;
   }
   if (0) {
      BEGIN_BATCH(1, IGNORE_CLIPRECTS);
      OUT_BATCH((CMD_MI_FLUSH << 16) | BRW_FLUSH_STATE_CACHE);
      ADVANCE_BATCH();
   }

   return 0;
}


/* May fail if out of video memory for texture or vbo upload, or on
 * fallback conditions.
 */
static int
try_draw_range_elements(struct brw_context *brw,
			struct pipe_buffer *index_buffer,
			unsigned hw_prim, 
			unsigned start, unsigned count)
{
   int ret;

   ret = brw_validate_state(brw);
   if (ret)
      return ret;

   /* Check that we can fit our state in with our existing batchbuffer, or
    * flush otherwise.
    */
   ret = brw->sws->check_aperture_space(brw->sws,
					brw->state.validated_bos,
					brw->state.validated_bo_count);
   if (ret)
      return ret;

   ret = brw_upload_state(brw);
   if (ret)
      return ret;
   
   ret = brw_emit_prim(brw, start, count, index_buffer != NULL, hw_prim);
   if (ret)
      return ret;

   if (brw->flags.always_flush_batch)
      brw_context_flush( brw );

   return 0;
}


static boolean
brw_draw_range_elements(struct pipe_context *pipe,
			struct pipe_buffer *index_buffer,
			unsigned index_size,
			unsigned min_index,
			unsigned max_index,
			unsigned mode, unsigned start, unsigned count)
{
   struct brw_context *brw = brw_context(pipe);
   int ret;
   uint32_t hw_prim;

   hw_prim = brw_set_prim(brw, mode);

   if (BRW_DEBUG & DEBUG_PRIMS)
      debug_printf("PRIM: %s %d %d\n", u_prim_name(mode), start, count);

   /* Potentially trigger upload of new index buffer.
    *
    * XXX: do we need to go through state validation to achieve this?
    * Could just call upload code directly.
    */
   if (brw->curr.index_buffer != index_buffer) {
      pipe_buffer_reference( &brw->curr.index_buffer, index_buffer );
      brw->state.dirty.mesa |= PIPE_NEW_INDEX_BUFFER;
   }

   /* XXX: do we really care?
    */
   if (brw->curr.min_index != min_index ||
       brw->curr.max_index != max_index) 
   { 
      brw->curr.min_index = min_index;
      brw->curr.max_index = max_index;
      brw->state.dirty.mesa |= PIPE_NEW_INDEX_RANGE;
   }


   /* Make a first attempt at drawing:
    */
   ret = try_draw_range_elements(brw, index_buffer, hw_prim, start, count );

   /* Otherwise, flush and retry:
    */
   if (ret != 0) {
      brw_context_flush( brw );
      ret = try_draw_range_elements(brw, index_buffer, hw_prim, start, count );
      assert(ret == 0);
   }

   return TRUE;
}

static boolean
brw_draw_elements(struct pipe_context *pipe,
		  struct pipe_buffer *index_buffer,
		  unsigned index_size,
		  unsigned mode, 
		  unsigned start, unsigned count)
{
   return brw_draw_range_elements( pipe, index_buffer,
				   index_size,
				   0, 0xffffffff,
				   mode, 
				   start, count );
}

static boolean
brw_draw_arrays(struct pipe_context *pipe, unsigned mode,
                     unsigned start, unsigned count)
{
   return brw_draw_elements(pipe, NULL, 0, mode, start, count);
}



boolean brw_draw_init( struct brw_context *brw )
{
   /* Register our drawing function: 
    */
   brw->base.draw_arrays = brw_draw_arrays;
   brw->base.draw_elements = brw_draw_elements;
   brw->base.draw_range_elements = brw_draw_range_elements;

   /* Create helpers for uploading data in user buffers:
    */
   brw->vb.upload_vertex = u_upload_create( &brw->brw_screen->base,
					    128 * 1024,
					    64,
					    PIPE_BUFFER_USAGE_VERTEX );
   if (brw->vb.upload_vertex == NULL)
      return FALSE;

   brw->vb.upload_index = u_upload_create( &brw->brw_screen->base,
					   128 * 1024,
					   64,
					   PIPE_BUFFER_USAGE_INDEX );
   if (brw->vb.upload_index == NULL)
      return FALSE;

   return TRUE;
}

void brw_draw_cleanup( struct brw_context *brw )
{
   u_upload_destroy( brw->vb.upload_vertex );
   u_upload_destroy( brw->vb.upload_index );

   brw->sws->bo_unreference(brw->ib.bo);
   brw->ib.bo = NULL;
}