1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "st_context.h"
29 #include "st_format.h"
30 #include "st_texture.h"
31 #include "st_cb_fbo.h"
32 #include "st_inlines.h"
33 #include "main/enums.h"
35 #undef Elements /* fix re-defined macro warning */
37 #include "pipe/p_state.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "util/u_inlines.h"
41 #include "util/u_format.h"
42 #include "util/u_rect.h"
43 #include "util/u_math.h"
46 #define DBG if(0) printf
50 * Allocate a new pipe_resource object
51 * width0, height0, depth0 are the dimensions of the level 0 image
52 * (the highest resolution). last_level indicates how many mipmap levels
53 * to allocate storage for. For non-mipmapped textures, this will be zero.
55 struct pipe_resource
*
56 st_texture_create(struct st_context
*st
,
57 enum pipe_texture_target target
,
58 enum pipe_format format
,
65 struct pipe_resource pt
, *newtex
;
66 struct pipe_screen
*screen
= st
->pipe
->screen
;
68 assert(target
<= PIPE_TEXTURE_CUBE
);
70 DBG("%s target %s format %s last_level %d\n", __FUNCTION__
,
71 _mesa_lookup_enum_by_nr(target
),
72 _mesa_lookup_enum_by_nr(format
), last_level
);
75 assert(screen
->is_format_supported(screen
, format
, target
, 0,
76 PIPE_BIND_SAMPLER_VIEW
, 0));
78 memset(&pt
, 0, sizeof(pt
));
81 pt
.last_level
= last_level
;
85 pt
.usage
= PIPE_USAGE_DEFAULT
;
89 newtex
= screen
->resource_create(screen
, &pt
);
91 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
98 * Check if a texture image can be pulled into a unified mipmap texture.
101 st_texture_match_image(const struct pipe_resource
*pt
,
102 const struct gl_texture_image
*image
,
103 GLuint face
, GLuint level
)
105 /* Images with borders are never pulled into mipmap textures.
110 /* Check if this image's format matches the established texture's format.
112 if (st_mesa_format_to_pipe_format(image
->TexFormat
) != pt
->format
)
115 /* Test if this image's size matches what's expected in the
116 * established texture.
118 if (image
->Width
!= u_minify(pt
->width0
, level
) ||
119 image
->Height
!= u_minify(pt
->height0
, level
) ||
120 image
->Depth
!= u_minify(pt
->depth0
, level
))
128 * Map a texture image and return the address for a particular 2D face/slice/
129 * layer. The stImage indicates the cube face and mipmap level. The slice
130 * of the 3D texture is passed in 'zoffset'.
131 * \param usage one of the PIPE_TRANSFER_x values
132 * \param x, y, w, h the region of interest of the 2D image.
133 * \return address of mapping or NULL if any error
136 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
137 GLuint zoffset
, enum pipe_transfer_usage usage
,
138 GLuint x
, GLuint y
, GLuint w
, GLuint h
)
140 struct pipe_context
*pipe
= st
->pipe
;
141 struct pipe_resource
*pt
= stImage
->pt
;
143 DBG("%s \n", __FUNCTION__
);
145 stImage
->transfer
= st_no_flush_get_tex_transfer(st
, pt
, stImage
->face
,
146 stImage
->level
, zoffset
,
149 if (stImage
->transfer
)
150 return pipe_transfer_map(pipe
, stImage
->transfer
);
157 st_texture_image_unmap(struct st_context
*st
,
158 struct st_texture_image
*stImage
)
160 struct pipe_context
*pipe
= st
->pipe
;
162 DBG("%s\n", __FUNCTION__
);
164 pipe_transfer_unmap(pipe
, stImage
->transfer
);
166 pipe
->transfer_destroy(pipe
, stImage
->transfer
);
172 * Upload data to a rectangular sub-region. Lots of choices how to do this:
174 * - memcpy by span to current destination
175 * - upload data as new buffer and blit
177 * Currently always memcpy.
180 st_surface_data(struct pipe_context
*pipe
,
181 struct pipe_transfer
*dst
,
182 unsigned dstx
, unsigned dsty
,
183 const void *src
, unsigned src_stride
,
184 unsigned srcx
, unsigned srcy
, unsigned width
, unsigned height
)
186 void *map
= pipe_transfer_map(pipe
, dst
);
188 assert(dst
->resource
);
190 dst
->resource
->format
,
197 pipe_transfer_unmap(pipe
, dst
);
201 /* Upload data for a particular image.
204 st_texture_image_data(struct st_context
*st
,
205 struct pipe_resource
*dst
,
209 GLuint src_row_stride
, GLuint src_image_stride
)
211 struct pipe_context
*pipe
= st
->pipe
;
212 GLuint depth
= u_minify(dst
->depth0
, level
);
214 const GLubyte
*srcUB
= src
;
215 struct pipe_transfer
*dst_transfer
;
217 DBG("%s\n", __FUNCTION__
);
219 for (i
= 0; i
< depth
; i
++) {
220 dst_transfer
= st_no_flush_get_tex_transfer(st
, dst
, face
, level
, i
,
221 PIPE_TRANSFER_WRITE
, 0, 0,
222 u_minify(dst
->width0
, level
),
223 u_minify(dst
->height0
, level
));
225 st_surface_data(pipe
, dst_transfer
,
226 0, 0, /* dstx, dsty */
229 0, 0, /* source x, y */
230 u_minify(dst
->width0
, level
),
231 u_minify(dst
->height0
, level
)); /* width, height */
233 pipe
->transfer_destroy(pipe
, dst_transfer
);
235 srcUB
+= src_image_stride
;
240 /* Copy mipmap image between textures
243 st_texture_image_copy(struct pipe_context
*pipe
,
244 struct pipe_resource
*dst
, GLuint dstLevel
,
245 struct pipe_resource
*src
,
248 GLuint width
= u_minify(dst
->width0
, dstLevel
);
249 GLuint height
= u_minify(dst
->height0
, dstLevel
);
250 GLuint depth
= u_minify(dst
->depth0
, dstLevel
);
251 struct pipe_subresource dstsub
, srcsub
;
254 assert(src
->width0
== dst
->width0
);
255 assert(src
->height0
== dst
->height0
);
258 dstsub
.level
= dstLevel
;
261 for (i
= 0; i
< depth
; i
++) {
264 /* find src texture level of needed size */
265 for (srcLevel
= 0; srcLevel
<= src
->last_level
; srcLevel
++) {
266 if (u_minify(src
->width0
, srcLevel
) == width
&&
267 u_minify(src
->height0
, srcLevel
) == height
) {
271 assert(u_minify(src
->width0
, srcLevel
) == width
);
272 assert(u_minify(src
->height0
, srcLevel
) == height
);
276 struct pipe_screen
*screen
= pipe
->screen
;
277 src_surface
= screen
->get_tex_surface(screen
, src
, face
, srcLevel
, i
,
278 PIPE_BUFFER_USAGE_CPU_READ
);
279 ubyte
*map
= screen
->surface_map(screen
, src_surface
, PIPE_BUFFER_USAGE_CPU_READ
);
280 map
+= src_surface
->width
* src_surface
->height
* 4 / 2;
281 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
283 map
[0], map
[1], map
[2], map
[3],
284 src
, srcLevel
, dst
, dstLevel
);
286 screen
->surface_unmap(screen
, src_surface
);
287 pipe_surface_reference(&src_surface
, NULL
);
290 srcsub
.level
= srcLevel
;
292 pipe
->resource_copy_region(pipe
,
295 0, 0, i
,/* destX, Y, Z */
298 0, 0, i
,/* srcX, Y, Z */
305 st_teximage_flush_before_map(struct st_context
*st
,
306 struct pipe_resource
*pt
,
309 enum pipe_transfer_usage usage
)
311 struct pipe_context
*pipe
= st
->pipe
;
312 unsigned referenced
=
313 pipe
->is_resource_referenced(pipe
, pt
, face
, level
);
315 if (referenced
&& ((referenced
& PIPE_REFERENCED_FOR_WRITE
) ||
316 (usage
& PIPE_TRANSFER_WRITE
)))
317 st
->pipe
->flush(st
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);