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 #include "i915_context.h"
27 #include "i915_reg.h"
28 #include "i915_resource.h"
29 #include "i915_screen.h"
30 #include "i915_state.h"
31
32 /***********************************************************************
33 * Update framebuffer state
34 */
35 static unsigned
translate_format(enum pipe_format format)36 translate_format(enum pipe_format format)
37 {
38 switch (format) {
39 case PIPE_FORMAT_B8G8R8A8_UNORM:
40 case PIPE_FORMAT_B8G8R8A8_SRGB:
41 case PIPE_FORMAT_B8G8R8X8_UNORM:
42 case PIPE_FORMAT_R8G8B8A8_UNORM:
43 case PIPE_FORMAT_R8G8B8X8_UNORM:
44 return COLOR_BUF_ARGB8888;
45 case PIPE_FORMAT_B5G6R5_UNORM:
46 return COLOR_BUF_RGB565;
47 case PIPE_FORMAT_B5G5R5A1_UNORM:
48 return COLOR_BUF_ARGB1555;
49 case PIPE_FORMAT_B4G4R4A4_UNORM:
50 return COLOR_BUF_ARGB4444;
51 case PIPE_FORMAT_B10G10R10A2_UNORM:
52 return COLOR_BUF_ARGB2101010;
53 case PIPE_FORMAT_L8_UNORM:
54 case PIPE_FORMAT_A8_UNORM:
55 case PIPE_FORMAT_I8_UNORM:
56 return COLOR_BUF_8BIT;
57 default:
58 assert(0);
59 return 0;
60 }
61 }
62
63 static unsigned
translate_depth_format(enum pipe_format zformat)64 translate_depth_format(enum pipe_format zformat)
65 {
66 switch (zformat) {
67 case PIPE_FORMAT_Z24X8_UNORM:
68 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
69 return DEPTH_FRMT_24_FIXED_8_OTHER;
70 case PIPE_FORMAT_Z16_UNORM:
71 return DEPTH_FRMT_16_FIXED;
72 default:
73 assert(0);
74 return 0;
75 }
76 }
77
78 static void
update_framebuffer(struct i915_context * i915)79 update_framebuffer(struct i915_context *i915)
80 {
81 struct pipe_surface *cbuf_surface = i915->framebuffer.cbufs[0];
82 struct pipe_surface *depth_surface = i915->framebuffer.zsbuf;
83 unsigned x, y;
84 unsigned y1;
85 int layer;
86 uint32_t draw_offset, draw_size;
87
88 if (cbuf_surface) {
89 struct i915_surface *surf = i915_surface(cbuf_surface);
90 struct i915_texture *tex = i915_texture(cbuf_surface->texture);
91 assert(tex);
92
93 i915->current.cbuf_bo = tex->buffer;
94 i915->current.cbuf_flags = surf->buf_info;
95 i915->current.cbuf_offset = 0;
96
97 layer = cbuf_surface->u.tex.first_layer;
98
99 x = tex->image_offset[cbuf_surface->u.tex.level][layer].nblocksx;
100 y = tex->image_offset[cbuf_surface->u.tex.level][layer].nblocksy;
101 // Use offset if buffer not within max texture size 2048
102 if (y + i915->framebuffer.height >= (1 << (I915_MAX_TEXTURE_2D_LEVELS - 1))) {
103 // offset should be multiple of 8 to support TILE_X
104 y1 = (y / 8) * 8;
105 y -= y1;
106 i915->current.cbuf_offset = y1 * tex->stride;
107 }
108 } else {
109 i915->current.cbuf_bo = NULL;
110 x = y = 0;
111 }
112 i915->static_dirty |= I915_DST_BUF_COLOR;
113
114 /* What happens if no zbuf??
115 */
116 if (depth_surface) {
117 struct i915_surface *surf = i915_surface(depth_surface);
118 struct i915_texture *tex = i915_texture(depth_surface->texture);
119 unsigned offset = i915_texture_offset(tex, depth_surface->u.tex.level,
120 depth_surface->u.tex.first_layer);
121 assert(tex);
122 if (offset != 0)
123 debug_printf("Depth offset is %d\n", offset);
124
125 i915->current.depth_bo = tex->buffer;
126 i915->current.depth_flags = surf->buf_info;
127 } else
128 i915->current.depth_bo = NULL;
129 i915->static_dirty |= I915_DST_BUF_DEPTH;
130
131 /* drawing rect calculations */
132 draw_offset = x | (y << 16);
133 draw_size = (i915->framebuffer.width - 1 + x) |
134 ((i915->framebuffer.height - 1 + y) << 16);
135 if (i915->current.draw_offset != draw_offset) {
136 i915->current.draw_offset = draw_offset;
137 i915_set_flush_dirty(i915, I915_PIPELINE_FLUSH);
138 i915->static_dirty |= I915_DST_RECT;
139 }
140 if (i915->current.draw_size != draw_size) {
141 i915->current.draw_size = draw_size;
142 i915->static_dirty |= I915_DST_RECT;
143 }
144
145 i915->hardware_dirty |= I915_HW_STATIC;
146
147 /* flush the cache in case we sample from the old renderbuffers */
148 i915_set_flush_dirty(i915, I915_FLUSH_CACHE);
149 }
150
151 struct i915_tracked_state i915_hw_framebuffer = {
152 "framebuffer", update_framebuffer, I915_NEW_FRAMEBUFFER};
153
154 static void
update_dst_buf_vars(struct i915_context * i915)155 update_dst_buf_vars(struct i915_context *i915)
156 {
157 struct pipe_surface *cbuf_surface = i915->framebuffer.cbufs[0];
158 struct pipe_surface *depth_surface = i915->framebuffer.zsbuf;
159 uint32_t dst_buf_vars, cformat, zformat;
160 uint32_t early_z = 0;
161
162 if (cbuf_surface)
163 cformat = cbuf_surface->format;
164 else
165 cformat = PIPE_FORMAT_B8G8R8A8_UNORM; /* arbitrary */
166 cformat = translate_format(cformat);
167
168 if (depth_surface) {
169 struct i915_texture *tex = i915_texture(depth_surface->texture);
170 struct i915_screen *is = i915_screen(i915->base.screen);
171
172 zformat = translate_depth_format(depth_surface->format);
173
174 if (is->is_i945 && tex->tiling != I915_TILE_NONE &&
175 (i915->fs && !i915->fs->info.writes_z))
176 early_z = CLASSIC_EARLY_DEPTH;
177 } else
178 zformat = 0;
179
180 dst_buf_vars = DSTORG_HORT_BIAS(0x8) | /* .5 */
181 DSTORG_VERT_BIAS(0x8) | /* .5 */
182 LOD_PRECLAMP_OGL | TEX_DEFAULT_COLOR_OGL | cformat | zformat |
183 early_z;
184
185 if (i915->current.dst_buf_vars != dst_buf_vars) {
186 if (early_z != (i915->current.dst_buf_vars & CLASSIC_EARLY_DEPTH))
187 i915_set_flush_dirty(i915, I915_PIPELINE_FLUSH);
188
189 i915->current.dst_buf_vars = dst_buf_vars;
190 i915->static_dirty |= I915_DST_VARS;
191 i915->hardware_dirty |= I915_HW_STATIC;
192 }
193 }
194
195 struct i915_tracked_state i915_hw_dst_buf_vars = {
196 "dst buf vars", update_dst_buf_vars, I915_NEW_FRAMEBUFFER | I915_NEW_FS};
197