llvmpipe: move rasterizer to screen instead of setup context
[profile/ivi/mesa.git] / src / gallium / drivers / i915 / i915_state_emit.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
29 #include "i915_reg.h"
30 #include "i915_context.h"
31 #include "i915_batch.h"
32 #include "i915_debug.h"
33 #include "i915_reg.h"
34 #include "i915_resource.h"
35
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38
39 static unsigned translate_format( enum pipe_format format )
40 {
41    switch (format) {
42    case PIPE_FORMAT_B8G8R8A8_UNORM:
43       return COLOR_BUF_ARGB8888;
44    case PIPE_FORMAT_B5G6R5_UNORM:
45       return COLOR_BUF_RGB565;
46    default:
47       assert(0);
48       return 0;
49    }
50 }
51
52 static unsigned translate_depth_format( enum pipe_format zformat )
53 {
54    switch (zformat) {
55    case PIPE_FORMAT_Z24X8_UNORM:
56    case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
57       return DEPTH_FRMT_24_FIXED_8_OTHER;
58    case PIPE_FORMAT_Z16_UNORM:
59       return DEPTH_FRMT_16_FIXED;
60    default:
61       assert(0);
62       return 0;
63    }
64 }
65
66
67 /**
68  * Examine framebuffer state to determine width, height.
69  */
70 static boolean
71 framebuffer_size(const struct pipe_framebuffer_state *fb,
72                  uint *width, uint *height)
73 {
74    if (fb->cbufs[0]) {
75       *width = fb->cbufs[0]->width;
76       *height = fb->cbufs[0]->height;
77       return TRUE;
78    }
79    else if (fb->zsbuf) {
80       *width = fb->zsbuf->width;
81       *height = fb->zsbuf->height;
82       return TRUE;
83    }
84    else {
85       *width = *height = 0;
86       return FALSE;
87    }
88 }
89
90
91 /* Push the state into the sarea and/or texture memory.
92  */
93 void
94 i915_emit_hardware_state(struct i915_context *i915 )
95 {
96    /* XXX: there must be an easier way */
97    const unsigned dwords = ( 14 + 
98                              7 + 
99                              I915_MAX_DYNAMIC + 
100                              8 + 
101                              2 + I915_TEX_UNITS*3 + 
102                              2 + I915_TEX_UNITS*3 +
103                              2 + I915_MAX_CONSTANT*4 + 
104 #if 0
105                              i915->current.program_len + 
106 #else
107                              i915->fs->program_len + 
108 #endif
109                              6 
110                            ) * 3/2; /* plus 50% margin */
111    const unsigned relocs = ( I915_TEX_UNITS +
112                              3
113                            ) * 3/2; /* plus 50% margin */
114
115    uintptr_t save_ptr;
116    size_t save_relocs;
117
118    if (I915_DBG_ON(DBG_ATOMS))
119       i915_dump_hardware_dirty(i915, __FUNCTION__);
120
121    if(!BEGIN_BATCH(dwords, relocs)) {
122       FLUSH_BATCH(NULL);
123       assert(BEGIN_BATCH(dwords, relocs));
124    }
125
126    save_ptr = (uintptr_t)i915->batch->ptr;
127    save_relocs = i915->batch->relocs;
128
129    /* 14 dwords, 0 relocs */
130    if (i915->hardware_dirty & I915_HW_INVARIENT)
131    {
132       OUT_BATCH(_3DSTATE_AA_CMD |
133                 AA_LINE_ECAAR_WIDTH_ENABLE |
134                 AA_LINE_ECAAR_WIDTH_1_0 |
135                 AA_LINE_REGION_WIDTH_ENABLE | AA_LINE_REGION_WIDTH_1_0);
136
137       OUT_BATCH(_3DSTATE_DFLT_DIFFUSE_CMD);
138       OUT_BATCH(0);
139
140       OUT_BATCH(_3DSTATE_DFLT_SPEC_CMD);
141       OUT_BATCH(0);
142       
143       OUT_BATCH(_3DSTATE_DFLT_Z_CMD);
144       OUT_BATCH(0);
145
146       OUT_BATCH(_3DSTATE_COORD_SET_BINDINGS |
147                 CSB_TCB(0, 0) |
148                 CSB_TCB(1, 1) |
149                 CSB_TCB(2, 2) |
150                 CSB_TCB(3, 3) |
151                 CSB_TCB(4, 4) | 
152                 CSB_TCB(5, 5) | 
153                 CSB_TCB(6, 6) | 
154                 CSB_TCB(7, 7));
155
156       OUT_BATCH(_3DSTATE_RASTER_RULES_CMD |
157                 ENABLE_POINT_RASTER_RULE |
158                 OGL_POINT_RASTER_RULE |
159                 ENABLE_LINE_STRIP_PROVOKE_VRTX |
160                 ENABLE_TRI_FAN_PROVOKE_VRTX |
161                 LINE_STRIP_PROVOKE_VRTX(1) |
162                 TRI_FAN_PROVOKE_VRTX(2) | 
163                 ENABLE_TEXKILL_3D_4D | 
164                 TEXKILL_4D);
165
166       /* Need to initialize this to zero.
167        */
168       OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(3) | (0));
169       OUT_BATCH(0);
170
171       OUT_BATCH(_3DSTATE_DEPTH_SUBRECT_DISABLE);
172
173       /* disable indirect state for now
174        */
175       OUT_BATCH(_3DSTATE_LOAD_INDIRECT | 0);
176       OUT_BATCH(0);
177    }
178
179    /* 7 dwords, 1 relocs */
180    if (i915->hardware_dirty & I915_HW_IMMEDIATE)
181    {
182       OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1 | 
183                 I1_LOAD_S(0) |
184                 I1_LOAD_S(1) |
185                 I1_LOAD_S(2) |
186                 I1_LOAD_S(4) |
187                 I1_LOAD_S(5) |
188                 I1_LOAD_S(6) | 
189                 (5));
190       
191       if(i915->vbo)
192          OUT_RELOC(i915->vbo,
193                    I915_USAGE_VERTEX,
194                    i915->current.immediate[I915_IMMEDIATE_S0]);
195       else
196          /* FIXME: we should not do this */
197          OUT_BATCH(0);
198       OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S1]);
199       OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S2]);
200       OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S4]);
201       OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S5]);
202       OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S6]);
203    } 
204
205 #if 01
206    /* I915_MAX_DYNAMIC dwords, 0 relocs */
207    if (i915->hardware_dirty & I915_HW_DYNAMIC) 
208    {
209       int i;
210       for (i = 0; i < I915_MAX_DYNAMIC; i++) {
211          OUT_BATCH(i915->current.dynamic[i]);
212       }
213    }
214 #endif
215
216 #if 01
217    /* 8 dwords, 2 relocs */
218    if (i915->hardware_dirty & I915_HW_STATIC)
219    {
220       struct pipe_surface *cbuf_surface = i915->framebuffer.cbufs[0];
221       struct pipe_surface *depth_surface = i915->framebuffer.zsbuf;
222
223       if (cbuf_surface) {
224          unsigned ctile = BUF_3D_USE_FENCE;
225          struct i915_texture *tex = i915_texture(cbuf_surface->texture);
226          assert(tex);
227
228          if (tex && tex->sw_tiled) {
229             ctile = BUF_3D_TILED_SURFACE;
230          }
231
232          OUT_BATCH(_3DSTATE_BUF_INFO_CMD);
233
234          OUT_BATCH(BUF_3D_ID_COLOR_BACK |
235                    BUF_3D_PITCH(tex->stride) |  /* pitch in bytes */
236                    ctile);
237
238          OUT_RELOC(tex->buffer,
239                    I915_USAGE_RENDER,
240                    cbuf_surface->offset);
241       }
242
243       /* What happens if no zbuf??
244        */
245       if (depth_surface) {
246          unsigned ztile = BUF_3D_USE_FENCE;
247          struct i915_texture *tex = i915_texture(depth_surface->texture);
248          assert(tex);
249
250          if (tex && tex->sw_tiled) {
251             ztile = BUF_3D_TILED_SURFACE;
252          }
253
254          OUT_BATCH(_3DSTATE_BUF_INFO_CMD);
255
256          assert(tex);
257          OUT_BATCH(BUF_3D_ID_DEPTH |
258                    BUF_3D_PITCH(tex->stride) |  /* pitch in bytes */
259                    ztile);
260
261          OUT_RELOC(tex->buffer,
262                    I915_USAGE_RENDER,
263                    depth_surface->offset);
264       }
265
266       {
267          unsigned cformat, zformat = 0;
268
269          if (cbuf_surface)
270             cformat = cbuf_surface->format;
271          else
272             cformat = PIPE_FORMAT_B8G8R8A8_UNORM; /* arbitrary */
273          cformat = translate_format(cformat);
274
275          if (depth_surface) 
276             zformat = translate_depth_format( i915->framebuffer.zsbuf->format );
277
278          OUT_BATCH(_3DSTATE_DST_BUF_VARS_CMD);
279          OUT_BATCH(DSTORG_HORT_BIAS(0x8) | /* .5 */
280                    DSTORG_VERT_BIAS(0x8) | /* .5 */
281                    LOD_PRECLAMP_OGL |
282                    TEX_DEFAULT_COLOR_OGL |
283                    cformat |
284                    zformat );
285       }
286    }
287 #endif
288
289 #if 01
290       /* texture images */
291       /* 2 + I915_TEX_UNITS*3 dwords, I915_TEX_UNITS relocs */
292       if (i915->hardware_dirty & (I915_HW_MAP | I915_HW_SAMPLER))
293       {
294          const uint nr = i915->current.sampler_enable_nr;
295          if (nr) {
296             const uint enabled = i915->current.sampler_enable_flags;
297             uint unit;
298             uint count = 0;
299             OUT_BATCH(_3DSTATE_MAP_STATE | (3 * nr));
300             OUT_BATCH(enabled);
301             for (unit = 0; unit < I915_TEX_UNITS; unit++) {
302                if (enabled & (1 << unit)) {
303                   struct i915_texture *texture = i915_texture(i915->fragment_sampler_views[unit]->texture);
304                   struct i915_winsys_buffer *buf = texture->buffer;
305                   uint offset = 0;
306                   assert(buf);
307
308                   count++;
309
310                   OUT_RELOC(buf, I915_USAGE_SAMPLER, offset);
311                   OUT_BATCH(i915->current.texbuffer[unit][0]); /* MS3 */
312                   OUT_BATCH(i915->current.texbuffer[unit][1]); /* MS4 */
313                }
314             }
315             assert(count == nr);
316          }
317       }
318 #endif
319
320 #if 01
321    /* samplers */
322    /* 2 + I915_TEX_UNITS*3 dwords, 0 relocs */
323    if (i915->hardware_dirty & I915_HW_SAMPLER) 
324    {
325       if (i915->current.sampler_enable_nr) {
326          int i;
327
328          OUT_BATCH( _3DSTATE_SAMPLER_STATE | 
329                     (3 * i915->current.sampler_enable_nr) );
330
331          OUT_BATCH( i915->current.sampler_enable_flags );
332
333          for (i = 0; i < I915_TEX_UNITS; i++) {
334             if (i915->current.sampler_enable_flags & (1<<i)) {
335                OUT_BATCH( i915->current.sampler[i][0] );
336                OUT_BATCH( i915->current.sampler[i][1] );
337                OUT_BATCH( i915->current.sampler[i][2] );
338             }
339          }
340       }
341    }
342 #endif
343
344 #if 01
345    /* constants */
346    /* 2 + I915_MAX_CONSTANT*4 dwords, 0 relocs */
347    if (i915->hardware_dirty & I915_HW_CONSTANTS)
348    {
349       /* Collate the user-defined constants with the fragment shader's
350        * immediates according to the constant_flags[] array.
351        */
352       const uint nr = i915->fs->num_constants;
353       if (nr) {
354          uint i;
355
356          OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS | (nr * 4) );
357          OUT_BATCH( (1 << (nr - 1)) | ((1 << (nr - 1)) - 1) );
358
359          for (i = 0; i < nr; i++) {
360             const uint *c;
361             if (i915->fs->constant_flags[i] == I915_CONSTFLAG_USER) {
362                /* grab user-defined constant */
363                c = (uint *) i915->current.constants[PIPE_SHADER_FRAGMENT][i];
364             }
365             else {
366                /* emit program constant */
367                c = (uint *) i915->fs->constants[i];
368             }
369 #if 0 /* debug */
370             {
371                float *f = (float *) c;
372                printf("Const %2d: %f %f %f %f %s\n", i, f[0], f[1], f[2], f[3],
373                       (i915->fs->constant_flags[i] == I915_CONSTFLAG_USER
374                        ? "user" : "immediate"));
375             }
376 #endif
377             OUT_BATCH(*c++);
378             OUT_BATCH(*c++);
379             OUT_BATCH(*c++);
380             OUT_BATCH(*c++);
381          }
382       }
383    }
384 #endif
385
386 #if 01
387    /* Fragment program */
388    /* i915->current.program_len dwords, 0 relocs */
389    if (i915->hardware_dirty & I915_HW_PROGRAM)
390    {
391       uint i;
392       /* we should always have, at least, a pass-through program */
393       assert(i915->fs->program_len > 0);
394       for (i = 0; i < i915->fs->program_len; i++) {
395          OUT_BATCH(i915->fs->program[i]);
396       }
397    }
398 #endif
399
400 #if 01
401    /* drawing surface size */
402    /* 6 dwords, 0 relocs */
403    {
404       uint w, h;
405       boolean k = framebuffer_size(&i915->framebuffer, &w, &h);
406       (void)k;
407       assert(k);
408
409       OUT_BATCH(_3DSTATE_DRAW_RECT_CMD);
410       OUT_BATCH(0);
411       OUT_BATCH(0);
412       OUT_BATCH(((w - 1) & 0xffff) | ((h - 1) << 16));
413       OUT_BATCH(0);
414       OUT_BATCH(0);
415    }
416 #endif
417
418    I915_DBG(DBG_EMIT, "%s: used %d dwords, %d relocs\n", __FUNCTION__,
419             ((uintptr_t)i915->batch->ptr - save_ptr) / 4,
420             i915->batch->relocs - save_relocs);
421
422    i915->hardware_dirty = 0;
423 }