gallium: rename clearRT / clearDS to clear_render_target / clear_depth_stencil
[profile/ivi/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, struct pipe_subresource subdst,
47                   unsigned dstx, unsigned dsty, unsigned dstz,
48                   struct pipe_resource *src, struct pipe_subresource subsrc,
49                   unsigned srcx, unsigned srcy, unsigned srcz,
50                   unsigned width, unsigned height)
51 {
52    struct i915_texture *dst_tex = i915_texture(dst);
53    struct i915_texture *src_tex = i915_texture(src);
54    struct pipe_resource *dpt = &dst_tex->b.b;
55    struct pipe_resource *spt = &src_tex->b.b;
56    unsigned dst_offset, src_offset;  /* in bytes */
57
58    if (dst->target == PIPE_TEXTURE_CUBE) {
59       dst_offset = dst_tex->image_offset[subdst.level][subdst.face];
60    }
61    else if (dst->target == PIPE_TEXTURE_3D) {
62       dst_offset = dst_tex->image_offset[subdst.level][dstz];
63    }
64    else {
65       dst_offset = dst_tex->image_offset[subdst.level][0];
66       assert(subdst.face == 0);
67       assert(dstz == 0);
68    }
69    if (src->target == PIPE_TEXTURE_CUBE) {
70       src_offset = src_tex->image_offset[subsrc.level][subsrc.face];
71    }
72    else if (src->target == PIPE_TEXTURE_3D) {
73       src_offset = src_tex->image_offset[subsrc.level][srcz];
74    }
75    else {
76       src_offset = src_tex->image_offset[subsrc.level][0];
77       assert(subsrc.face == 0);
78       assert(srcz == 0);
79    }
80
81
82    assert( dst != src );
83    assert( util_format_get_blocksize(dpt->format) == util_format_get_blocksize(spt->format) );
84    assert( util_format_get_blockwidth(dpt->format) == util_format_get_blockwidth(spt->format) );
85    assert( util_format_get_blockheight(dpt->format) == util_format_get_blockheight(spt->format) );
86    assert( util_format_get_blockwidth(dpt->format) == 1 );
87    assert( util_format_get_blockheight(dpt->format) == 1 );
88
89    i915_copy_blit( i915_context(pipe),
90                    util_format_get_blocksize(dpt->format),
91                    (unsigned short) src_tex->stride, src_tex->buffer, src_offset,
92                    (unsigned short) dst_tex->stride, dst_tex->buffer, dst_offset,
93                    (short) srcx, (short) srcy, (short) dstx, (short) dsty, (short) width, (short) height );
94 }
95
96
97 static void
98 i915_clear_render_target(struct pipe_context *pipe,
99                          struct pipe_surface *dst,
100                          const float *rgba,
101                          unsigned dstx, unsigned dsty,
102                          unsigned width, unsigned height)
103 {
104    struct i915_texture *tex = i915_texture(dst->texture);
105    struct pipe_resource *pt = &tex->b.b;
106    union util_color uc;
107
108    assert(util_format_get_blockwidth(pt->format) == 1);
109    assert(util_format_get_blockheight(pt->format) == 1);
110
111    util_pack_color(rgba, dst->format, &uc);
112    i915_fill_blit( i915_context(pipe),
113                    util_format_get_blocksize(pt->format),
114                    XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB,
115                    (unsigned short) tex->stride,
116                    tex->buffer, dst->offset,
117                    (short) dstx, (short) dsty,
118                    (short) width, (short) height,
119                    uc.ui );
120 }
121
122 static void
123 i915_clear_depth_stencil(struct pipe_context *pipe,
124                          struct pipe_surface *dst,
125                          unsigned clear_flags,
126                          double depth,
127                          unsigned stencil,
128                          unsigned dstx, unsigned dsty,
129                          unsigned width, unsigned height)
130 {
131    struct i915_texture *tex = i915_texture(dst->texture);
132    struct pipe_resource *pt = &tex->b.b;
133    unsigned packedds;
134    unsigned mask = 0;
135
136    assert(util_format_get_blockwidth(pt->format) == 1);
137    assert(util_format_get_blockheight(pt->format) == 1);
138
139    packedds = util_pack_z_stencil(dst->format, depth, stencil);
140
141    if (clear_flags & PIPE_CLEAR_DEPTH)
142       mask |= XY_COLOR_BLT_WRITE_RGB;
143    /* XXX presumably this does read-modify-write
144       (otherwise this won't work anyway). Hence will only want to
145       do it if really have stencil and it isn't cleared */
146    if (!((clear_flags & PIPE_CLEAR_STENCIL) ||
147        (dst->format != PIPE_FORMAT_Z24_UNORM_S8_USCALED)))
148       mask |= XY_COLOR_BLT_WRITE_ALPHA;
149
150    i915_fill_blit( i915_context(pipe),
151                    util_format_get_blocksize(pt->format),
152                    mask,
153                    (unsigned short) tex->stride,
154                    tex->buffer, dst->offset,
155                    (short) dstx, (short) dsty,
156                    (short) width, (short) height,
157                    packedds );
158 }
159
160 /*
161  * Screen surface functions
162  */
163
164
165 static struct pipe_surface *
166 i915_get_tex_surface(struct pipe_screen *screen,
167                      struct pipe_resource *pt,
168                      unsigned face, unsigned level, unsigned zslice,
169                      unsigned flags)
170 {
171    struct i915_texture *tex = i915_texture(pt);
172    struct pipe_surface *ps;
173    unsigned offset;  /* in bytes */
174
175    if (pt->target == PIPE_TEXTURE_CUBE) {
176       offset = tex->image_offset[level][face];
177    }
178    else if (pt->target == PIPE_TEXTURE_3D) {
179       offset = tex->image_offset[level][zslice];
180    }
181    else {
182       offset = tex->image_offset[level][0];
183       assert(face == 0);
184       assert(zslice == 0);
185    }
186
187    ps = CALLOC_STRUCT(pipe_surface);
188    if (ps) {
189       pipe_reference_init(&ps->reference, 1);
190       pipe_resource_reference(&ps->texture, pt);
191       ps->format = pt->format;
192       ps->width = u_minify(pt->width0, level);
193       ps->height = u_minify(pt->height0, level);
194       ps->offset = offset;
195       ps->usage = flags;
196    }
197    return ps;
198 }
199
200 static void
201 i915_tex_surface_destroy(struct pipe_surface *surf)
202 {
203    pipe_resource_reference(&surf->texture, NULL);
204    FREE(surf);
205 }
206
207
208 void
209 i915_init_surface_functions(struct i915_context *i915)
210 {
211    i915->base.resource_copy_region = i915_surface_copy;
212    i915->base.clear_render_target = i915_clear_render_target;
213    i915->base.clear_depth_stencil = i915_clear_depth_stencil;
214 }
215
216 /* No good reason for these to be in the screen.
217  */
218 void
219 i915_init_screen_surface_functions(struct i915_screen *is)
220 {
221    is->base.get_tex_surface = i915_get_tex_surface;
222    is->base.tex_surface_destroy = i915_tex_surface_destroy;
223 }