1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 **************************************************************************/
30 #include "st_context.h"
31 #include "st_format.h"
32 #include "st_texture.h"
33 #include "st_cb_fbo.h"
34 #include "main/enums.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "util/u_inlines.h"
40 #include "util/u_format.h"
41 #include "util/u_rect.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.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
,
67 struct pipe_resource pt
, *newtex
;
68 struct pipe_screen
*screen
= st
->pipe
->screen
;
70 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
74 if (target
== PIPE_TEXTURE_CUBE
)
77 DBG("%s target %d format %s last_level %d\n", __func__
,
78 (int) target
, util_format_name(format
), last_level
);
81 assert(screen
->is_format_supported(screen
, format
, target
, 0,
82 PIPE_BIND_SAMPLER_VIEW
));
84 memset(&pt
, 0, sizeof(pt
));
87 pt
.last_level
= last_level
;
91 pt
.array_size
= layers
;
92 pt
.usage
= PIPE_USAGE_DEFAULT
;
94 /* only set this for OpenGL textures, not renderbuffers */
95 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
96 pt
.nr_samples
= nr_samples
;
98 newtex
= screen
->resource_create(screen
, &pt
);
100 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
107 * In OpenGL the number of 1D array texture layers is the "height" and
108 * the number of 2D array texture layers is the "depth". In Gallium the
109 * number of layers in an array texture is a separate 'array_size' field.
110 * This function converts dimensions from the former to the later.
113 st_gl_texture_dims_to_pipe_dims(GLenum texture
,
124 case GL_PROXY_TEXTURE_1D
:
125 assert(heightIn
== 1);
126 assert(depthIn
== 1);
132 case GL_TEXTURE_1D_ARRAY
:
133 case GL_PROXY_TEXTURE_1D_ARRAY
:
134 assert(depthIn
== 1);
138 *layersOut
= heightIn
;
141 case GL_PROXY_TEXTURE_2D
:
142 case GL_TEXTURE_RECTANGLE
:
143 case GL_PROXY_TEXTURE_RECTANGLE
:
144 case GL_TEXTURE_EXTERNAL_OES
:
145 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
146 case GL_TEXTURE_2D_MULTISAMPLE
:
147 assert(depthIn
== 1);
149 *heightOut
= heightIn
;
153 case GL_TEXTURE_CUBE_MAP
:
154 case GL_PROXY_TEXTURE_CUBE_MAP
:
155 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
156 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
157 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
158 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
159 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
160 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
161 assert(depthIn
== 1);
163 *heightOut
= heightIn
;
167 case GL_TEXTURE_2D_ARRAY
:
168 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
169 case GL_PROXY_TEXTURE_2D_ARRAY
:
170 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
172 *heightOut
= heightIn
;
174 *layersOut
= depthIn
;
176 case GL_TEXTURE_CUBE_MAP_ARRAY
:
177 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
179 *heightOut
= heightIn
;
181 *layersOut
= util_align_npot(depthIn
, 6);
184 assert(0 && "Unexpected texture in st_gl_texture_dims_to_pipe_dims()");
187 case GL_PROXY_TEXTURE_3D
:
189 *heightOut
= heightIn
;
198 * Check if a texture image can be pulled into a unified mipmap texture.
201 st_texture_match_image(struct st_context
*st
,
202 const struct pipe_resource
*pt
,
203 const struct gl_texture_image
*image
)
205 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
207 /* Images with borders are never pulled into mipmap textures.
212 /* Check if this image's format matches the established texture's format.
214 if (st_mesa_format_to_pipe_format(st
, image
->TexFormat
) != pt
->format
)
217 st_gl_texture_dims_to_pipe_dims(image
->TexObject
->Target
,
218 image
->Width
, image
->Height
, image
->Depth
,
219 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
221 /* Test if this image's size matches what's expected in the
222 * established texture.
224 if (ptWidth
!= u_minify(pt
->width0
, image
->Level
) ||
225 ptHeight
!= u_minify(pt
->height0
, image
->Level
) ||
226 ptDepth
!= u_minify(pt
->depth0
, image
->Level
) ||
227 ptLayers
!= pt
->array_size
)
230 if (image
->Level
> pt
->last_level
)
238 * Map a texture image and return the address for a particular 2D face/slice/
239 * layer. The stImage indicates the cube face and mipmap level. The slice
240 * of the 3D texture is passed in 'zoffset'.
241 * \param usage one of the PIPE_TRANSFER_x values
242 * \param x, y, w, h the region of interest of the 2D image.
243 * \return address of mapping or NULL if any error
246 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
247 enum pipe_transfer_usage usage
,
248 GLuint x
, GLuint y
, GLuint z
,
249 GLuint w
, GLuint h
, GLuint d
,
250 struct pipe_transfer
**transfer
)
252 struct st_texture_object
*stObj
=
253 st_texture_object(stImage
->base
.TexObject
);
257 DBG("%s \n", __func__
);
262 if (stObj
->pt
!= stImage
->pt
)
265 level
= stImage
->base
.Level
;
267 if (stObj
->base
.Immutable
) {
268 level
+= stObj
->base
.MinLevel
;
269 z
+= stObj
->base
.MinLayer
;
270 if (stObj
->pt
->array_size
> 1)
271 d
= MIN2(d
, stObj
->base
.NumLayers
);
274 z
+= stImage
->base
.Face
;
276 map
= pipe_transfer_map_3d(st
->pipe
, stImage
->pt
, level
, usage
,
277 x
, y
, z
, w
, h
, d
, transfer
);
279 /* Enlarge the transfer array if it's not large enough. */
280 if (z
>= stImage
->num_transfers
) {
281 unsigned new_size
= z
+ 1;
283 stImage
->transfer
= realloc(stImage
->transfer
,
284 new_size
* sizeof(struct st_texture_image_transfer
));
285 memset(&stImage
->transfer
[stImage
->num_transfers
], 0,
286 (new_size
- stImage
->num_transfers
) *
287 sizeof(struct st_texture_image_transfer
));
288 stImage
->num_transfers
= new_size
;
291 assert(!stImage
->transfer
[z
].transfer
);
292 stImage
->transfer
[z
].transfer
= *transfer
;
299 st_texture_image_unmap(struct st_context
*st
,
300 struct st_texture_image
*stImage
, unsigned slice
)
302 struct pipe_context
*pipe
= st
->pipe
;
303 struct st_texture_object
*stObj
=
304 st_texture_object(stImage
->base
.TexObject
);
305 struct pipe_transfer
**transfer
;
307 if (stObj
->base
.Immutable
)
308 slice
+= stObj
->base
.MinLayer
;
309 transfer
= &stImage
->transfer
[slice
+ stImage
->base
.Face
].transfer
;
311 DBG("%s\n", __func__
);
313 pipe_transfer_unmap(pipe
, *transfer
);
318 * For debug only: get/print center pixel in the src resource.
321 print_center_pixel(struct pipe_context
*pipe
, struct pipe_resource
*src
)
323 struct pipe_transfer
*xfer
;
324 struct pipe_box region
;
327 region
.x
= src
->width0
/ 2;
328 region
.y
= src
->height0
/ 2;
334 map
= pipe
->transfer_map(pipe
, src
, 0, PIPE_TRANSFER_READ
, ®ion
, &xfer
);
336 printf("center pixel: %d %d %d %d\n", map
[0], map
[1], map
[2], map
[3]);
338 pipe
->transfer_unmap(pipe
, xfer
);
343 * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'.
344 * This is used to copy mipmap images from one texture buffer to another.
345 * This typically happens when our initial guess at the total texture size
346 * is incorrect (see the guess_and_alloc_texture() function).
349 st_texture_image_copy(struct pipe_context
*pipe
,
350 struct pipe_resource
*dst
, GLuint dstLevel
,
351 struct pipe_resource
*src
, GLuint srcLevel
,
354 GLuint width
= u_minify(dst
->width0
, dstLevel
);
355 GLuint height
= u_minify(dst
->height0
, dstLevel
);
356 GLuint depth
= u_minify(dst
->depth0
, dstLevel
);
357 struct pipe_box src_box
;
360 if (u_minify(src
->width0
, srcLevel
) != width
||
361 u_minify(src
->height0
, srcLevel
) != height
||
362 u_minify(src
->depth0
, srcLevel
) != depth
) {
363 /* The source image size doesn't match the destination image size.
364 * This can happen in some degenerate situations such as rendering to a
365 * cube map face which was set up with mismatched texture sizes.
372 src_box
.width
= width
;
373 src_box
.height
= height
;
376 if (src
->target
== PIPE_TEXTURE_1D_ARRAY
||
377 src
->target
== PIPE_TEXTURE_2D_ARRAY
||
378 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
380 depth
= src
->array_size
;
383 /* Loop over 3D image slices */
384 /* could (and probably should) use "true" 3d box here -
385 but drivers can't quite handle it yet */
386 for (i
= face
; i
< face
+ depth
; i
++) {
390 print_center_pixel(pipe
, src
);
393 pipe
->resource_copy_region(pipe
,
396 0, 0, i
,/* destX, Y, Z */
404 struct pipe_resource
*
405 st_create_color_map_texture(struct gl_context
*ctx
)
407 struct st_context
*st
= st_context(ctx
);
408 struct pipe_resource
*pt
;
409 enum pipe_format format
;
410 const uint texSize
= 256; /* simple, and usually perfect */
412 /* find an RGBA texture format */
413 format
= st_choose_format(st
, GL_RGBA
, GL_NONE
, GL_NONE
,
414 PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
,
417 /* create texture for color map/table */
418 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, format
, 0,
419 texSize
, texSize
, 1, 1, 0, PIPE_BIND_SAMPLER_VIEW
);