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"
34 #include "main/texfetch.h"
35 #include "main/teximage.h"
36 #include "main/texobj.h"
38 #undef Elements /* fix re-defined macro warning */
40 #include "pipe/p_state.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "util/u_inlines.h"
44 #include "util/u_format.h"
45 #include "util/u_rect.h"
46 #include "util/u_math.h"
49 #define DBG if(0) printf
53 target_to_target(GLenum target
)
56 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
57 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
58 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
59 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
60 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
61 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
62 return GL_TEXTURE_CUBE_MAP_ARB
;
71 * Allocate a new pipe_resource object
72 * width0, height0, depth0 are the dimensions of the level 0 image
73 * (the highest resolution). last_level indicates how many mipmap levels
74 * to allocate storage for. For non-mipmapped textures, this will be zero.
76 struct pipe_resource
*
77 st_texture_create(struct st_context
*st
,
78 enum pipe_texture_target target
,
79 enum pipe_format format
,
86 struct pipe_resource pt
, *newtex
;
87 struct pipe_screen
*screen
= st
->pipe
->screen
;
89 assert(target
<= PIPE_TEXTURE_CUBE
);
91 DBG("%s target %s format %s last_level %d\n", __FUNCTION__
,
92 _mesa_lookup_enum_by_nr(target
),
93 _mesa_lookup_enum_by_nr(format
), last_level
);
96 assert(screen
->is_format_supported(screen
, format
, target
,
97 PIPE_BIND_SAMPLER_VIEW
, 0));
99 memset(&pt
, 0, sizeof(pt
));
102 pt
.last_level
= last_level
;
104 pt
.height0
= height0
;
106 pt
._usage
= PIPE_USAGE_DEFAULT
;
110 newtex
= screen
->resource_create(screen
, &pt
);
112 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
119 * Check if a texture image can be pulled into a unified mipmap texture.
122 st_texture_match_image(const struct pipe_resource
*pt
,
123 const struct gl_texture_image
*image
,
124 GLuint face
, GLuint level
)
126 /* Images with borders are never pulled into mipmap textures.
131 /* Check if this image's format matches the established texture's format.
133 if (st_mesa_format_to_pipe_format(image
->TexFormat
) != pt
->format
)
136 /* Test if this image's size matches what's expected in the
137 * established texture.
139 if (image
->Width
!= u_minify(pt
->width0
, level
) ||
140 image
->Height
!= u_minify(pt
->height0
, level
) ||
141 image
->Depth
!= u_minify(pt
->depth0
, level
))
149 /* Although we use the image_offset[] array to store relative offsets
150 * to cube faces, Mesa doesn't know anything about this and expects
151 * each cube face to be treated as a separate image.
153 * These functions present that view to mesa:
156 st_texture_depth_offsets(struct pipe_resource
*pt
, GLuint level
)
158 static const GLuint zero
= 0;
160 if (pt
->target
!= PIPE_TEXTURE_3D
|| pt
->level
[level
].nr_images
== 1)
163 return pt
->level
[level
].image_offset
;
168 * Return the offset to the given mipmap texture image within the
169 * texture memory buffer, in bytes.
172 st_texture_image_offset(const struct pipe_resource
* pt
,
173 GLuint face
, GLuint level
)
175 if (pt
->target
== PIPE_TEXTURE_CUBE
)
176 return (pt
->level
[level
].level_offset
+
177 pt
->level
[level
].image_offset
[face
] * pt
->cpp
);
179 return pt
->level
[level
].level_offset
;
185 * Map a teximage in a mipmap texture.
186 * \param row_stride returns row stride in bytes
187 * \param image_stride returns image stride in bytes (for 3D textures).
188 * \return address of mapping
191 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
192 GLuint zoffset
, enum pipe_transfer_usage usage
,
193 GLuint x
, GLuint y
, GLuint w
, GLuint h
)
195 struct pipe_context
*pipe
= st
->pipe
;
196 struct pipe_resource
*pt
= stImage
->pt
;
198 DBG("%s \n", __FUNCTION__
);
200 stImage
->transfer
= st_no_flush_get_tex_transfer(st
, pt
, stImage
->face
,
201 stImage
->level
, zoffset
,
204 if (stImage
->transfer
)
205 return pipe_transfer_map(pipe
, stImage
->transfer
);
212 st_texture_image_unmap(struct st_context
*st
,
213 struct st_texture_image
*stImage
)
215 struct pipe_context
*pipe
= st
->pipe
;
217 DBG("%s\n", __FUNCTION__
);
219 pipe_transfer_unmap(pipe
, stImage
->transfer
);
221 pipe
->transfer_destroy(pipe
, stImage
->transfer
);
227 * Upload data to a rectangular sub-region. Lots of choices how to do this:
229 * - memcpy by span to current destination
230 * - upload data as new buffer and blit
232 * Currently always memcpy.
235 st_surface_data(struct pipe_context
*pipe
,
236 struct pipe_transfer
*dst
,
237 unsigned dstx
, unsigned dsty
,
238 const void *src
, unsigned src_stride
,
239 unsigned srcx
, unsigned srcy
, unsigned width
, unsigned height
)
241 void *map
= pipe_transfer_map(pipe
, dst
);
243 assert(dst
->resource
);
245 dst
->resource
->format
,
252 pipe_transfer_unmap(pipe
, dst
);
256 /* Upload data for a particular image.
259 st_texture_image_data(struct st_context
*st
,
260 struct pipe_resource
*dst
,
264 GLuint src_row_stride
, GLuint src_image_stride
)
266 struct pipe_context
*pipe
= st
->pipe
;
267 GLuint depth
= u_minify(dst
->depth0
, level
);
269 const GLubyte
*srcUB
= src
;
270 struct pipe_transfer
*dst_transfer
;
272 DBG("%s\n", __FUNCTION__
);
274 for (i
= 0; i
< depth
; i
++) {
275 dst_transfer
= st_no_flush_get_tex_transfer(st
, dst
, face
, level
, i
,
276 PIPE_TRANSFER_WRITE
, 0, 0,
277 u_minify(dst
->width0
, level
),
278 u_minify(dst
->height0
, level
));
280 st_surface_data(pipe
, dst_transfer
,
281 0, 0, /* dstx, dsty */
284 0, 0, /* source x, y */
285 u_minify(dst
->width0
, level
),
286 u_minify(dst
->height0
, level
)); /* width, height */
288 pipe
->transfer_destroy(pipe
, dst_transfer
);
290 srcUB
+= src_image_stride
;
295 /* Copy mipmap image between textures
298 st_texture_image_copy(struct pipe_context
*pipe
,
299 struct pipe_resource
*dst
, GLuint dstLevel
,
300 struct pipe_resource
*src
,
303 struct pipe_screen
*screen
= pipe
->screen
;
304 GLuint width
= u_minify(dst
->width0
, dstLevel
);
305 GLuint height
= u_minify(dst
->height0
, dstLevel
);
306 GLuint depth
= u_minify(dst
->depth0
, dstLevel
);
307 struct pipe_surface
*src_surface
;
308 struct pipe_surface
*dst_surface
;
311 for (i
= 0; i
< depth
; i
++) {
314 /* find src texture level of needed size */
315 for (srcLevel
= 0; srcLevel
<= src
->last_level
; srcLevel
++) {
316 if (u_minify(src
->width0
, srcLevel
) == width
&&
317 u_minify(src
->height0
, srcLevel
) == height
) {
321 assert(u_minify(src
->width0
, srcLevel
) == width
);
322 assert(u_minify(src
->height0
, srcLevel
) == height
);
326 src_surface
= screen
->get_tex_surface(screen
, src
, face
, srcLevel
, i
,
327 PIPE_BUFFER_USAGE_CPU_READ
);
328 ubyte
*map
= screen
->surface_map(screen
, src_surface
, PIPE_BUFFER_USAGE_CPU_READ
);
329 map
+= src_surface
->width
* src_surface
->height
* 4 / 2;
330 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
332 map
[0], map
[1], map
[2], map
[3],
333 src
, srcLevel
, dst
, dstLevel
);
335 screen
->surface_unmap(screen
, src_surface
);
336 pipe_surface_reference(&src_surface
, NULL
);
340 dst_surface
= screen
->get_tex_surface(screen
, dst
, face
, dstLevel
, i
,
341 PIPE_BIND_BLIT_DESTINATION
);
343 src_surface
= screen
->get_tex_surface(screen
, src
, face
, srcLevel
, i
,
344 PIPE_BIND_BLIT_SOURCE
);
346 pipe
->surface_copy(pipe
,
353 pipe_surface_reference(&src_surface
, NULL
);
354 pipe_surface_reference(&dst_surface
, NULL
);
360 st_teximage_flush_before_map(struct st_context
*st
,
361 struct pipe_resource
*pt
,
364 enum pipe_transfer_usage usage
)
366 struct pipe_context
*pipe
= st
->pipe
;
367 unsigned referenced
=
368 pipe
->is_resource_referenced(pipe
, pt
, face
, level
);
370 if (referenced
&& ((referenced
& PIPE_REFERENCED_FOR_WRITE
) ||
371 (usage
& PIPE_TRANSFER_WRITE
)))
372 st
->pipe
->flush(st
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);