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