• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2014 Intel Corporation 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 "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR 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
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Jason Ekstrand <jason.ekstrand@intel.com>
26  */
27 
28 #include "brw_blorp.h"
29 #include "intel_fbo.h"
30 #include "intel_tex.h"
31 #include "intel_blit.h"
32 #include "intel_mipmap_tree.h"
33 #include "main/formats.h"
34 #include "main/teximage.h"
35 #include "drivers/common/meta.h"
36 
37 static void
copy_image_with_memcpy(struct brw_context * brw,struct intel_mipmap_tree * src_mt,int src_level,int src_x,int src_y,int src_z,struct intel_mipmap_tree * dst_mt,int dst_level,int dst_x,int dst_y,int dst_z,int src_width,int src_height)38 copy_image_with_memcpy(struct brw_context *brw,
39                        struct intel_mipmap_tree *src_mt, int src_level,
40                        int src_x, int src_y, int src_z,
41                        struct intel_mipmap_tree *dst_mt, int dst_level,
42                        int dst_x, int dst_y, int dst_z,
43                        int src_width, int src_height)
44 {
45    bool same_slice;
46    void *mapped, *src_mapped, *dst_mapped;
47    ptrdiff_t src_stride, dst_stride, cpp;
48    int map_x1, map_y1, map_x2, map_y2;
49    GLuint src_bw, src_bh;
50 
51    cpp = _mesa_get_format_bytes(src_mt->format);
52    _mesa_get_format_block_size(src_mt->format, &src_bw, &src_bh);
53 
54    assert(src_width % src_bw == 0);
55    assert(src_height % src_bh == 0);
56    assert(src_x % src_bw == 0);
57    assert(src_y % src_bh == 0);
58 
59    /* If we are on the same miptree, same level, and same slice, then
60     * intel_miptree_map won't let us map it twice.  We have to do things a
61     * bit differently.  In particular, we do a single map large enough for
62     * both portions and in read-write mode.
63     */
64    same_slice = src_mt == dst_mt && src_level == dst_level && src_z == dst_z;
65 
66    if (same_slice) {
67       assert(dst_x % src_bw == 0);
68       assert(dst_y % src_bh == 0);
69 
70       map_x1 = MIN2(src_x, dst_x);
71       map_y1 = MIN2(src_y, dst_y);
72       map_x2 = MAX2(src_x, dst_x) + src_width;
73       map_y2 = MAX2(src_y, dst_y) + src_height;
74 
75       intel_miptree_map(brw, src_mt, src_level, src_z,
76                         map_x1, map_y1, map_x2 - map_x1, map_y2 - map_y1,
77                         GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
78                         &mapped, &src_stride);
79 
80       dst_stride = src_stride;
81 
82       /* Set the offsets here so we don't have to think about while looping */
83       src_mapped = mapped + ((src_y - map_y1) / src_bh) * src_stride +
84                             ((src_x - map_x1) / src_bw) * cpp;
85       dst_mapped = mapped + ((dst_y - map_y1) / src_bh) * dst_stride +
86                             ((dst_x - map_x1) / src_bw) * cpp;
87    } else {
88       intel_miptree_map(brw, src_mt, src_level, src_z,
89                         src_x, src_y, src_width, src_height,
90                         GL_MAP_READ_BIT, &src_mapped, &src_stride);
91       intel_miptree_map(brw, dst_mt, dst_level, dst_z,
92                         dst_x, dst_y, src_width, src_height,
93                         GL_MAP_WRITE_BIT, &dst_mapped, &dst_stride);
94    }
95 
96    src_width /= (int)src_bw;
97    src_height /= (int)src_bh;
98 
99    for (int i = 0; i < src_height; ++i) {
100       memcpy(dst_mapped, src_mapped, src_width * cpp);
101       src_mapped += src_stride;
102       dst_mapped += dst_stride;
103    }
104 
105    if (same_slice) {
106       intel_miptree_unmap(brw, src_mt, src_level, src_z);
107    } else {
108       intel_miptree_unmap(brw, dst_mt, dst_level, dst_z);
109       intel_miptree_unmap(brw, src_mt, src_level, src_z);
110    }
111 }
112 
113 static void
copy_miptrees(struct brw_context * brw,struct intel_mipmap_tree * src_mt,int src_x,int src_y,int src_z,unsigned src_level,struct intel_mipmap_tree * dst_mt,int dst_x,int dst_y,int dst_z,unsigned dst_level,int src_width,int src_height)114 copy_miptrees(struct brw_context *brw,
115               struct intel_mipmap_tree *src_mt,
116               int src_x, int src_y, int src_z, unsigned src_level,
117               struct intel_mipmap_tree *dst_mt,
118               int dst_x, int dst_y, int dst_z, unsigned dst_level,
119               int src_width, int src_height)
120 {
121    unsigned bw, bh;
122 
123    if (brw->gen >= 6) {
124       brw_blorp_copy_miptrees(brw,
125                               src_mt, src_level, src_z,
126                               dst_mt, dst_level, dst_z,
127                               src_x, src_y, dst_x, dst_y,
128                               src_width, src_height);
129       return;
130    }
131 
132    /* We are now going to try and copy the texture using the blitter.  If
133     * that fails, we will fall back mapping the texture and using memcpy.
134     * In either case, we need to do a full resolve.
135     */
136    intel_miptree_all_slices_resolve_hiz(brw, src_mt);
137    intel_miptree_all_slices_resolve_depth(brw, src_mt);
138    intel_miptree_all_slices_resolve_color(brw, src_mt, 0);
139 
140    intel_miptree_all_slices_resolve_hiz(brw, dst_mt);
141    intel_miptree_all_slices_resolve_depth(brw, dst_mt);
142    intel_miptree_all_slices_resolve_color(brw, dst_mt, 0);
143 
144    _mesa_get_format_block_size(src_mt->format, &bw, &bh);
145 
146    /* It's legal to have a WxH that's smaller than a compressed block. This
147     * happens for example when you are using a higher level LOD. For this case,
148     * we still want to copy the entire block, or else the decompression will be
149     * incorrect.
150     */
151    if (src_width < bw)
152       src_width = ALIGN_NPOT(src_width, bw);
153 
154    if (src_height < bh)
155       src_height = ALIGN_NPOT(src_height, bh);
156 
157    if (intel_miptree_copy(brw, src_mt, src_level, src_z, src_x, src_y,
158                           dst_mt, dst_level, dst_z, dst_x, dst_y,
159                           src_width, src_height))
160       return;
161 
162    /* This is a worst-case scenario software fallback that maps the two
163     * textures and does a memcpy between them.
164     */
165    copy_image_with_memcpy(brw, src_mt, src_level,
166                           src_x, src_y, src_z,
167                           dst_mt, dst_level,
168                           dst_x, dst_y, dst_z,
169                           src_width, src_height);
170 }
171 
172 static void
intel_copy_image_sub_data(struct gl_context * ctx,struct gl_texture_image * src_image,struct gl_renderbuffer * src_renderbuffer,int src_x,int src_y,int src_z,struct gl_texture_image * dst_image,struct gl_renderbuffer * dst_renderbuffer,int dst_x,int dst_y,int dst_z,int src_width,int src_height)173 intel_copy_image_sub_data(struct gl_context *ctx,
174                           struct gl_texture_image *src_image,
175                           struct gl_renderbuffer *src_renderbuffer,
176                           int src_x, int src_y, int src_z,
177                           struct gl_texture_image *dst_image,
178                           struct gl_renderbuffer *dst_renderbuffer,
179                           int dst_x, int dst_y, int dst_z,
180                           int src_width, int src_height)
181 {
182    struct brw_context *brw = brw_context(ctx);
183    struct intel_mipmap_tree *src_mt, *dst_mt;
184    unsigned src_level, dst_level;
185 
186    if (src_image) {
187       src_mt = intel_texture_image(src_image)->mt;
188       src_level = src_image->Level + src_image->TexObject->MinLevel;
189 
190       /* Cube maps actually have different images per face */
191       if (src_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
192          src_z = src_image->Face;
193 
194       src_z += src_image->TexObject->MinLayer;
195    } else {
196       assert(src_renderbuffer);
197       src_mt = intel_renderbuffer(src_renderbuffer)->mt;
198       src_image = src_renderbuffer->TexImage;
199       src_level = 0;
200    }
201 
202    if (dst_image) {
203       dst_mt = intel_texture_image(dst_image)->mt;
204 
205       dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
206 
207       /* Cube maps actually have different images per face */
208       if (dst_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
209          dst_z = dst_image->Face;
210 
211       dst_z += dst_image->TexObject->MinLayer;
212    } else {
213       assert(dst_renderbuffer);
214       dst_mt = intel_renderbuffer(dst_renderbuffer)->mt;
215       dst_image = dst_renderbuffer->TexImage;
216       dst_level = 0;
217    }
218 
219    copy_miptrees(brw, src_mt, src_x, src_y, src_z, src_level,
220                  dst_mt, dst_x, dst_y, dst_z, dst_level,
221                  src_width, src_height);
222 
223    /* CopyImage only works for equal formats, texture view equivalence
224     * classes, and a couple special cases for compressed textures.
225     *
226     * Notably, GL_DEPTH_STENCIL does not appear in any equivalence
227     * classes, so we know the formats must be the same, and thus both
228     * will either have stencil, or not.  They can't be mismatched.
229     */
230    assert((src_mt->stencil_mt != NULL) == (dst_mt->stencil_mt != NULL));
231 
232    if (dst_mt->stencil_mt) {
233       copy_miptrees(brw, src_mt->stencil_mt, src_x, src_y, src_z, src_level,
234                     dst_mt->stencil_mt, dst_x, dst_y, dst_z, dst_level,
235                     src_width, src_height);
236    }
237 }
238 
239 void
intelInitCopyImageFuncs(struct dd_function_table * functions)240 intelInitCopyImageFuncs(struct dd_function_table *functions)
241 {
242    functions->CopyImageSubData = intel_copy_image_sub_data;
243 }
244