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", __FUNCTION__
,
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
= (target
== PIPE_TEXTURE_CUBE
? 6 : layers
);
92 pt
.usage
= PIPE_USAGE_DEFAULT
;
95 pt
.nr_samples
= nr_samples
;
97 newtex
= screen
->resource_create(screen
, &pt
);
99 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
106 * In OpenGL the number of 1D array texture layers is the "height" and
107 * the number of 2D array texture layers is the "depth". In Gallium the
108 * number of layers in an array texture is a separate 'array_size' field.
109 * This function converts dimensions from the former to the later.
112 st_gl_texture_dims_to_pipe_dims(GLenum texture
,
123 case GL_PROXY_TEXTURE_1D
:
124 assert(heightIn
== 1);
125 assert(depthIn
== 1);
131 case GL_TEXTURE_1D_ARRAY
:
132 case GL_PROXY_TEXTURE_1D_ARRAY
:
133 assert(depthIn
== 1);
137 *layersOut
= heightIn
;
140 case GL_PROXY_TEXTURE_2D
:
141 case GL_TEXTURE_RECTANGLE
:
142 case GL_PROXY_TEXTURE_RECTANGLE
:
143 case GL_TEXTURE_EXTERNAL_OES
:
144 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
145 case GL_TEXTURE_2D_MULTISAMPLE
:
146 assert(depthIn
== 1);
148 *heightOut
= heightIn
;
152 case GL_TEXTURE_CUBE_MAP
:
153 case GL_PROXY_TEXTURE_CUBE_MAP
:
154 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
155 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
156 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
157 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
158 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
159 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
160 assert(depthIn
== 1);
162 *heightOut
= heightIn
;
166 case GL_TEXTURE_2D_ARRAY
:
167 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
168 case GL_PROXY_TEXTURE_2D_ARRAY
:
169 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
171 *heightOut
= heightIn
;
173 *layersOut
= depthIn
;
175 case GL_TEXTURE_CUBE_MAP_ARRAY
:
176 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
178 *heightOut
= heightIn
;
180 *layersOut
= depthIn
;
183 assert(0 && "Unexpected texture in st_gl_texture_dims_to_pipe_dims()");
186 case GL_PROXY_TEXTURE_3D
:
188 *heightOut
= heightIn
;
197 * Check if a texture image can be pulled into a unified mipmap texture.
200 st_texture_match_image(const struct pipe_resource
*pt
,
201 const struct gl_texture_image
*image
)
203 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
205 /* Images with borders are never pulled into mipmap textures.
210 /* Check if this image's format matches the established texture's format.
212 if (st_mesa_format_to_pipe_format(image
->TexFormat
) != pt
->format
)
215 st_gl_texture_dims_to_pipe_dims(image
->TexObject
->Target
,
216 image
->Width
, image
->Height
, image
->Depth
,
217 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
219 /* Test if this image's size matches what's expected in the
220 * established texture.
222 if (ptWidth
!= u_minify(pt
->width0
, image
->Level
) ||
223 ptHeight
!= u_minify(pt
->height0
, image
->Level
) ||
224 ptDepth
!= u_minify(pt
->depth0
, image
->Level
) ||
225 ptLayers
!= pt
->array_size
)
233 * Map a texture image and return the address for a particular 2D face/slice/
234 * layer. The stImage indicates the cube face and mipmap level. The slice
235 * of the 3D texture is passed in 'zoffset'.
236 * \param usage one of the PIPE_TRANSFER_x values
237 * \param x, y, w, h the region of interest of the 2D image.
238 * \return address of mapping or NULL if any error
241 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
242 enum pipe_transfer_usage usage
,
243 GLuint x
, GLuint y
, GLuint z
,
244 GLuint w
, GLuint h
, GLuint d
)
246 struct st_texture_object
*stObj
=
247 st_texture_object(stImage
->base
.TexObject
);
250 DBG("%s \n", __FUNCTION__
);
255 if (stObj
->pt
!= stImage
->pt
)
258 level
= stImage
->base
.Level
;
260 return pipe_transfer_map_3d(st
->pipe
, stImage
->pt
, level
, usage
,
261 x
, y
, z
+ stImage
->base
.Face
,
262 w
, h
, d
, &stImage
->transfer
);
267 st_texture_image_unmap(struct st_context
*st
,
268 struct st_texture_image
*stImage
)
270 struct pipe_context
*pipe
= st
->pipe
;
272 DBG("%s\n", __FUNCTION__
);
274 pipe_transfer_unmap(pipe
, stImage
->transfer
);
275 stImage
->transfer
= NULL
;
279 /* Upload data for a particular image.
282 st_texture_image_data(struct st_context
*st
,
283 struct pipe_resource
*dst
,
287 GLuint src_row_stride
, GLuint src_image_stride
)
289 struct pipe_context
*pipe
= st
->pipe
;
291 const GLubyte
*srcUB
= src
;
294 if (dst
->target
== PIPE_TEXTURE_1D_ARRAY
||
295 dst
->target
== PIPE_TEXTURE_2D_ARRAY
||
296 dst
->target
== PIPE_TEXTURE_CUBE_ARRAY
)
297 layers
= dst
->array_size
;
299 layers
= u_minify(dst
->depth0
, level
);
301 DBG("%s\n", __FUNCTION__
);
303 for (i
= 0; i
< layers
; i
++) {
305 u_box_2d_zslice(0, 0, face
+ i
,
306 u_minify(dst
->width0
, level
),
307 u_minify(dst
->height0
, level
),
310 pipe
->transfer_inline_write(pipe
, dst
, level
, PIPE_TRANSFER_WRITE
,
311 &box
, srcUB
, src_row_stride
, 0);
313 srcUB
+= src_image_stride
;
319 * For debug only: get/print center pixel in the src resource.
322 print_center_pixel(struct pipe_context
*pipe
, struct pipe_resource
*src
)
324 struct pipe_transfer
*xfer
;
325 struct pipe_box region
;
328 region
.x
= src
->width0
/ 2;
329 region
.y
= src
->height0
/ 2;
335 map
= pipe
->transfer_map(pipe
, src
, 0, PIPE_TRANSFER_READ
, ®ion
, &xfer
);
337 printf("center pixel: %d %d %d %d\n", map
[0], map
[1], map
[2], map
[3]);
339 pipe
->transfer_unmap(pipe
, xfer
);
344 * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'.
345 * This is used to copy mipmap images from one texture buffer to another.
346 * This typically happens when our initial guess at the total texture size
347 * is incorrect (see the guess_and_alloc_texture() function).
350 st_texture_image_copy(struct pipe_context
*pipe
,
351 struct pipe_resource
*dst
, GLuint dstLevel
,
352 struct pipe_resource
*src
, GLuint srcLevel
,
355 GLuint width
= u_minify(dst
->width0
, dstLevel
);
356 GLuint height
= u_minify(dst
->height0
, dstLevel
);
357 GLuint depth
= u_minify(dst
->depth0
, dstLevel
);
358 struct pipe_box src_box
;
361 if (u_minify(src
->width0
, srcLevel
) != width
||
362 u_minify(src
->height0
, srcLevel
) != height
||
363 u_minify(src
->depth0
, srcLevel
) != depth
) {
364 /* The source image size doesn't match the destination image size.
365 * This can happen in some degenerate situations such as rendering to a
366 * cube map face which was set up with mismatched texture sizes.
373 src_box
.width
= width
;
374 src_box
.height
= height
;
376 /* Loop over 3D image slices */
377 /* could (and probably should) use "true" 3d box here -
378 but drivers can't quite handle it yet */
379 for (i
= face
; i
< face
+ depth
; i
++) {
383 print_center_pixel(pipe
, src
);
386 pipe
->resource_copy_region(pipe
,
389 0, 0, i
,/* destX, Y, Z */
397 struct pipe_resource
*
398 st_create_color_map_texture(struct gl_context
*ctx
)
400 struct st_context
*st
= st_context(ctx
);
401 struct pipe_resource
*pt
;
402 enum pipe_format format
;
403 const uint texSize
= 256; /* simple, and usually perfect */
405 /* find an RGBA texture format */
406 format
= st_choose_format(st
, GL_RGBA
, GL_NONE
, GL_NONE
,
407 PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
,
410 /* create texture for color map/table */
411 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, format
, 0,
412 texSize
, texSize
, 1, 1, 0, PIPE_BIND_SAMPLER_VIEW
);
417 * Try to find a matching sampler view for the given context.
418 * If none is found an empty slot is initialized with a
419 * template and returned instead.
421 struct pipe_sampler_view
**
422 st_texture_get_sampler_view(struct st_context
*st
,
423 struct st_texture_object
*stObj
)
425 struct pipe_sampler_view
**used
= NULL
, **free
= NULL
;
428 for (i
= 0; i
< stObj
->num_sampler_views
; ++i
) {
429 struct pipe_sampler_view
**sv
= &stObj
->sampler_views
[i
];
430 /* Is the array entry used ? */
432 /* Yes, check if it's the right one */
433 if ((*sv
)->context
== st
->pipe
)
436 /* Wasn't the right one, but remember it as template */
439 /* Found a free slot, remember that */
444 /* Couldn't find a slot for our context, create a new one */
447 /* Haven't even found a free one, resize the array */
448 GLuint old_size
= stObj
->num_sampler_views
* sizeof(void *);
449 GLuint new_size
= old_size
+ sizeof(void *);
450 stObj
->sampler_views
= REALLOC(stObj
->sampler_views
, old_size
, new_size
);
451 free
= &stObj
->sampler_views
[stObj
->num_sampler_views
++];
455 /* Add just any sampler view to be used as a template */
457 pipe_sampler_view_reference(free
, *used
);
463 st_texture_release_sampler_view(struct st_context
*st
,
464 struct st_texture_object
*stObj
)
468 for (i
= 0; i
< stObj
->num_sampler_views
; ++i
) {
469 struct pipe_sampler_view
**sv
= &stObj
->sampler_views
[i
];
471 if (*sv
&& (*sv
)->context
== st
->pipe
) {
472 pipe_sampler_view_reference(sv
, NULL
);
479 st_texture_release_all_sampler_views(struct st_texture_object
*stObj
)
483 for (i
= 0; i
< stObj
->num_sampler_views
; ++i
)
484 pipe_sampler_view_reference(&stObj
->sampler_views
[i
], NULL
);
489 st_texture_free_sampler_views(struct st_texture_object
*stObj
)
492 * We use FREE() here to match REALLOC() above. Both come from
493 * u_memory.h, not imports.h. If we mis-match MALLOC/FREE from
494 * those two headers we can trash the heap.
496 FREE(stObj
->sampler_views
);