i915g: implement hw clear
[mesa.git] / src / gallium / drivers / i915 / i915_state_static.c
1 /**************************************************************************
2 *
3 * Copyright © 2010 Jakob Bornecrantz
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27 #include "i915_reg.h"
28 #include "i915_context.h"
29 #include "i915_state.h"
30 #include "i915_resource.h"
31
32
33
34 /***********************************************************************
35 * Update framebuffer state
36 */
37 static unsigned translate_format(enum pipe_format format)
38 {
39 switch (format) {
40 case PIPE_FORMAT_B8G8R8A8_UNORM:
41 return COLOR_BUF_ARGB8888;
42 case PIPE_FORMAT_B5G6R5_UNORM:
43 return COLOR_BUF_RGB565;
44 default:
45 assert(0);
46 return 0;
47 }
48 }
49
50 static unsigned translate_depth_format(enum pipe_format zformat)
51 {
52 switch (zformat) {
53 case PIPE_FORMAT_Z24X8_UNORM:
54 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
55 return DEPTH_FRMT_24_FIXED_8_OTHER;
56 case PIPE_FORMAT_Z16_UNORM:
57 return DEPTH_FRMT_16_FIXED;
58 default:
59 assert(0);
60 return 0;
61 }
62 }
63
64 static inline uint32_t
65 buf_3d_tiling_bits(enum i915_winsys_buffer_tile tiling)
66 {
67 uint32_t tiling_bits = 0;
68
69 switch (tiling) {
70 case I915_TILE_Y:
71 tiling_bits |= BUF_3D_TILE_WALK_Y;
72 case I915_TILE_X:
73 tiling_bits |= BUF_3D_TILED_SURFACE;
74 case I915_TILE_NONE:
75 break;
76 }
77
78 return tiling_bits;
79 }
80
81 /**
82 * Examine framebuffer state to determine width, height.
83 */
84 static boolean
85 framebuffer_size(const struct pipe_framebuffer_state *fb,
86 uint *width, uint *height)
87 {
88 if (fb->cbufs[0]) {
89 *width = fb->cbufs[0]->width;
90 *height = fb->cbufs[0]->height;
91 return TRUE;
92 }
93 else if (fb->zsbuf) {
94 *width = fb->zsbuf->width;
95 *height = fb->zsbuf->height;
96 return TRUE;
97 }
98 else {
99 *width = *height = 0;
100 return FALSE;
101 }
102 }
103
104 static void update_framebuffer(struct i915_context *i915)
105 {
106 struct pipe_surface *cbuf_surface = i915->framebuffer.cbufs[0];
107 struct pipe_surface *depth_surface = i915->framebuffer.zsbuf;
108 unsigned cformat, zformat;
109 unsigned x, y, w, h;
110 int layer;
111 uint32_t draw_offset;
112 boolean ret;
113
114 if (cbuf_surface) {
115 struct i915_texture *tex = i915_texture(cbuf_surface->texture);
116 assert(tex);
117
118 i915->current.cbuf_bo = tex->buffer;
119 i915->current.cbuf_flags = BUF_3D_ID_COLOR_BACK |
120 BUF_3D_PITCH(tex->stride) | /* pitch in bytes */
121 buf_3d_tiling_bits(tex->tiling);
122 cformat = cbuf_surface->format;
123
124 layer = cbuf_surface->u.tex.first_layer;
125
126 x = tex->image_offset[cbuf_surface->u.tex.level][layer].nblocksx;
127 y = tex->image_offset[cbuf_surface->u.tex.level][layer].nblocksy;
128 } else {
129 i915->current.cbuf_bo = NULL;
130 cformat = PIPE_FORMAT_B8G8R8A8_UNORM; /* arbitrary */
131 x = y = 0;
132 }
133 cformat = translate_format(cformat);
134
135 /* What happens if no zbuf??
136 */
137 if (depth_surface) {
138 struct i915_texture *tex = i915_texture(depth_surface->texture);
139 unsigned offset = i915_texture_offset(tex, depth_surface->u.tex.level,
140 depth_surface->u.tex.first_layer);
141 assert(tex);
142 assert(offset == 0);
143
144 i915->current.depth_bo = tex->buffer;
145 i915->current.depth_flags = BUF_3D_ID_DEPTH |
146 BUF_3D_PITCH(tex->stride) | /* pitch in bytes */
147 buf_3d_tiling_bits(tex->tiling);
148 zformat = translate_depth_format(depth_surface->format);
149 } else {
150 i915->current.depth_bo = NULL;
151 zformat = 0;
152 }
153
154 i915->current.dst_buf_vars = DSTORG_HORT_BIAS(0x8) | /* .5 */
155 DSTORG_VERT_BIAS(0x8) | /* .5 */
156 LOD_PRECLAMP_OGL |
157 TEX_DEFAULT_COLOR_OGL |
158 cformat |
159 zformat;
160
161 /* drawing rect calculations */
162 draw_offset = x | (y << 16);
163 ret = framebuffer_size(&i915->framebuffer, &w, &h);
164 assert(ret);
165 if (i915->current.draw_offset != draw_offset) {
166 i915->current.draw_offset = draw_offset;
167 i915_set_flush_dirty(i915, I915_PIPELINE_FLUSH);
168 }
169 i915->current.draw_size = (w - 1 + x) | ((h - 1 + y) << 16);
170 i915->current.fb_height = h;
171 i915->current.fb_width = w;
172
173 i915->hardware_dirty |= I915_HW_STATIC;
174
175 /* flush the cache in case we sample from the old renderbuffers */
176 i915_set_flush_dirty(i915, I915_FLUSH_CACHE);
177 }
178
179 struct i915_tracked_state i915_hw_framebuffer = {
180 "framebuffer",
181 update_framebuffer,
182 I915_NEW_FRAMEBUFFER
183 };