r600g: avoid useless shader rebuild at draw call
[mesa.git] / src / gallium / drivers / i915 / i915_surface.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 #include "i915_surface.h"
29 #include "i915_resource.h"
30 #include "i915_blit.h"
31 #include "i915_reg.h"
32 #include "i915_screen.h"
33 #include "pipe/p_defines.h"
34 #include "util/u_inlines.h"
35 #include "util/u_math.h"
36 #include "util/u_format.h"
37 #include "util/u_memory.h"
38 #include "util/u_pack_color.h"
39
40
41 /* Assumes all values are within bounds -- no checking at this level -
42 * do it higher up if required.
43 */
44 static void
45 i915_surface_copy(struct pipe_context *pipe,
46 struct pipe_resource *dst, unsigned dst_level,
47 unsigned dstx, unsigned dsty, unsigned dstz,
48 struct pipe_resource *src, unsigned src_level,
49 const struct pipe_box *src_box)
50 {
51 struct i915_texture *dst_tex = i915_texture(dst);
52 struct i915_texture *src_tex = i915_texture(src);
53 struct pipe_resource *dpt = &dst_tex->b.b;
54 struct pipe_resource *spt = &src_tex->b.b;
55 unsigned dst_offset, src_offset; /* in bytes */
56
57 /* XXX cannot copy 3d regions at this time */
58 assert(src_box->depth == 1);
59 if (dst->target != PIPE_TEXTURE_CUBE &&
60 dst->target != PIPE_TEXTURE_3D)
61 assert(dstz == 0);
62 dst_offset = i915_texture_offset(dst_tex, dst_level, dstz);
63
64 if (src->target != PIPE_TEXTURE_CUBE &&
65 src->target != PIPE_TEXTURE_3D)
66 assert(src_box->z == 0);
67 src_offset = i915_texture_offset(src_tex, src_level, src_box->z);
68
69 assert( dst != src );
70 assert( util_format_get_blocksize(dpt->format) == util_format_get_blocksize(spt->format) );
71 assert( util_format_get_blockwidth(dpt->format) == util_format_get_blockwidth(spt->format) );
72 assert( util_format_get_blockheight(dpt->format) == util_format_get_blockheight(spt->format) );
73 assert( util_format_get_blockwidth(dpt->format) == 1 );
74 assert( util_format_get_blockheight(dpt->format) == 1 );
75
76 i915_copy_blit( i915_context(pipe),
77 util_format_get_blocksize(dpt->format),
78 (unsigned short) src_tex->stride, src_tex->buffer, src_offset,
79 (unsigned short) dst_tex->stride, dst_tex->buffer, dst_offset,
80 (short) src_box->x, (short) src_box->y, (short) dstx, (short) dsty,
81 (short) src_box->width, (short) src_box->height );
82 }
83
84
85 static void
86 i915_clear_render_target(struct pipe_context *pipe,
87 struct pipe_surface *dst,
88 const float *rgba,
89 unsigned dstx, unsigned dsty,
90 unsigned width, unsigned height)
91 {
92 struct i915_texture *tex = i915_texture(dst->texture);
93 struct pipe_resource *pt = &tex->b.b;
94 union util_color uc;
95 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
96
97 assert(util_format_get_blockwidth(pt->format) == 1);
98 assert(util_format_get_blockheight(pt->format) == 1);
99
100 util_pack_color(rgba, dst->format, &uc);
101 i915_fill_blit( i915_context(pipe),
102 util_format_get_blocksize(pt->format),
103 XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB,
104 (unsigned short) tex->stride,
105 tex->buffer, offset,
106 (short) dstx, (short) dsty,
107 (short) width, (short) height,
108 uc.ui );
109 }
110
111 static void
112 i915_clear_depth_stencil(struct pipe_context *pipe,
113 struct pipe_surface *dst,
114 unsigned clear_flags,
115 double depth,
116 unsigned stencil,
117 unsigned dstx, unsigned dsty,
118 unsigned width, unsigned height)
119 {
120 struct i915_texture *tex = i915_texture(dst->texture);
121 struct pipe_resource *pt = &tex->b.b;
122 unsigned packedds;
123 unsigned mask = 0;
124 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
125
126 assert(util_format_get_blockwidth(pt->format) == 1);
127 assert(util_format_get_blockheight(pt->format) == 1);
128
129 packedds = util_pack_z_stencil(dst->format, depth, stencil);
130
131 if (clear_flags & PIPE_CLEAR_DEPTH)
132 mask |= XY_COLOR_BLT_WRITE_RGB;
133 /* XXX presumably this does read-modify-write
134 (otherwise this won't work anyway). Hence will only want to
135 do it if really have stencil and it isn't cleared */
136 if ((clear_flags & PIPE_CLEAR_STENCIL) ||
137 (dst->format != PIPE_FORMAT_Z24_UNORM_S8_USCALED))
138 mask |= XY_COLOR_BLT_WRITE_ALPHA;
139
140 i915_fill_blit( i915_context(pipe),
141 util_format_get_blocksize(pt->format),
142 mask,
143 (unsigned short) tex->stride,
144 tex->buffer, offset,
145 (short) dstx, (short) dsty,
146 (short) width, (short) height,
147 packedds );
148 }
149
150 /*
151 * Screen surface functions
152 */
153
154
155 static struct pipe_surface *
156 i915_create_surface(struct pipe_context *ctx,
157 struct pipe_resource *pt,
158 const struct pipe_surface *surf_tmpl)
159 {
160 struct pipe_surface *ps;
161
162 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
163 if (pt->target != PIPE_TEXTURE_CUBE &&
164 pt->target != PIPE_TEXTURE_3D)
165 assert(surf_tmpl->u.tex.first_layer == 0);
166
167 ps = CALLOC_STRUCT(pipe_surface);
168 if (ps) {
169 /* could subclass pipe_surface and store offset as it used to do */
170 pipe_reference_init(&ps->reference, 1);
171 pipe_resource_reference(&ps->texture, pt);
172 ps->format = surf_tmpl->format;
173 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
174 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
175 ps->u.tex.level = surf_tmpl->u.tex.level;
176 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
177 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
178 ps->usage = surf_tmpl->usage;
179 ps->context = ctx;
180 }
181 return ps;
182 }
183
184 static void
185 i915_surface_destroy(struct pipe_context *ctx,
186 struct pipe_surface *surf)
187 {
188 pipe_resource_reference(&surf->texture, NULL);
189 FREE(surf);
190 }
191
192
193 void
194 i915_init_surface_functions(struct i915_context *i915)
195 {
196 i915->base.resource_copy_region = i915_surface_copy;
197 i915->base.clear_render_target = i915_clear_render_target;
198 i915->base.clear_depth_stencil = i915_clear_depth_stencil;
199 i915->base.create_surface = i915_create_surface;
200 i915->base.surface_destroy = i915_surface_destroy;
201 }