2 * Mesa 3-D graphics library
4 * Copyright (C) 2011 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 "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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Functions for mapping/unmapping texture images.
29 #include "main/context.h"
30 #include "main/teximage.h"
31 #include "swrast/swrast.h"
32 #include "swrast/s_context.h"
36 * Allocate a new swrast_texture_image (a subclass of gl_texture_image).
37 * Called via ctx->Driver.NewTextureImage().
39 struct gl_texture_image
*
40 _swrast_new_texture_image( struct gl_context
*ctx
)
43 return (struct gl_texture_image
*) CALLOC_STRUCT(swrast_texture_image
);
48 * Free a swrast_texture_image (a subclass of gl_texture_image).
49 * Called via ctx->Driver.DeleteTextureImage().
52 _swrast_delete_texture_image(struct gl_context
*ctx
,
53 struct gl_texture_image
*texImage
)
55 /* Nothing special for the subclass yet */
56 _mesa_delete_texture_image(ctx
, texImage
);
61 * Called via ctx->Driver.AllocTextureImageBuffer()
64 _swrast_alloc_texture_image_buffer(struct gl_context
*ctx
,
65 struct gl_texture_image
*texImage
,
66 gl_format format
, GLsizei width
,
67 GLsizei height
, GLsizei depth
)
69 struct swrast_texture_image
*swImg
= swrast_texture_image(texImage
);
70 GLuint bytes
= _mesa_format_image_size(format
, width
, height
, depth
);
73 /* This _should_ be true (revisit if these ever fail) */
74 assert(texImage
->Width
== width
);
75 assert(texImage
->Height
== height
);
76 assert(texImage
->Depth
== depth
);
78 assert(!swImg
->Buffer
);
79 swImg
->Buffer
= _mesa_align_malloc(bytes
, 512);
83 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
84 swImg
->RowStride
= width
;
86 /* Allocate the ImageOffsets array and initialize to typical values.
87 * We allocate the array for 1D/2D textures too in order to avoid special-
88 * case code in the texstore routines.
90 swImg
->ImageOffsets
= (GLuint
*) malloc(depth
* sizeof(GLuint
));
91 if (!swImg
->ImageOffsets
)
94 for (i
= 0; i
< depth
; i
++) {
95 swImg
->ImageOffsets
[i
] = i
* width
* height
;
98 _swrast_init_texture_image(texImage
, width
, height
, depth
);
105 * Code that overrides ctx->Driver.AllocTextureImageBuffer may use this to
106 * initialize the fields of swrast_texture_image without allocating the image
107 * buffer or initializing ImageOffsets or RowStride.
109 * Returns GL_TRUE on success, GL_FALSE on memory allocation failure.
112 _swrast_init_texture_image(struct gl_texture_image
*texImage
, GLsizei width
,
113 GLsizei height
, GLsizei depth
)
115 struct swrast_texture_image
*swImg
= swrast_texture_image(texImage
);
117 if ((width
== 1 || _mesa_is_pow_two(texImage
->Width2
)) &&
118 (height
== 1 || _mesa_is_pow_two(texImage
->Height2
)) &&
119 (depth
== 1 || _mesa_is_pow_two(texImage
->Depth2
)))
120 swImg
->_IsPowerOfTwo
= GL_TRUE
;
122 swImg
->_IsPowerOfTwo
= GL_FALSE
;
124 /* Compute Width/Height/DepthScale for mipmap lod computation */
125 swImg
->WidthScale
= (GLfloat
) texImage
->Width
;
126 swImg
->HeightScale
= (GLfloat
) texImage
->Height
;
127 swImg
->DepthScale
= (GLfloat
) texImage
->Depth
;
132 * Called via ctx->Driver.FreeTextureImageBuffer()
135 _swrast_free_texture_image_buffer(struct gl_context
*ctx
,
136 struct gl_texture_image
*texImage
)
138 struct swrast_texture_image
*swImage
= swrast_texture_image(texImage
);
139 if (swImage
->Buffer
) {
140 _mesa_align_free(swImage
->Buffer
);
141 swImage
->Buffer
= NULL
;
144 if (swImage
->ImageOffsets
) {
145 free(swImage
->ImageOffsets
);
146 swImage
->ImageOffsets
= NULL
;
152 * Error checking for debugging only.
155 _mesa_check_map_teximage(struct gl_texture_image
*texImage
,
156 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
)
159 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D
)
160 assert(y
== 0 && h
== 1);
162 assert(x
< texImage
->Width
|| texImage
->Width
== 0);
163 assert(y
< texImage
->Height
|| texImage
->Height
== 0);
164 assert(x
+ w
<= texImage
->Width
);
165 assert(y
+ h
<= texImage
->Height
);
169 * Map a 2D slice of a texture image into user space.
170 * (x,y,w,h) defines a region of interest (ROI). Reading/writing texels
171 * outside of the ROI is undefined.
173 * \param texImage the texture image
174 * \param slice the 3D image slice or array texture slice
175 * \param x, y, w, h region of interest
176 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
177 * \param mapOut returns start of mapping of region of interest
178 * \param rowStrideOut returns row stride (in bytes)
181 _swrast_map_teximage(struct gl_context
*ctx
,
182 struct gl_texture_image
*texImage
,
184 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
189 struct swrast_texture_image
*swImage
= swrast_texture_image(texImage
);
191 GLint stride
, texelSize
;
194 _mesa_check_map_teximage(texImage
, slice
, x
, y
, w
, h
);
196 texelSize
= _mesa_get_format_bytes(texImage
->TexFormat
);
197 stride
= _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
198 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
203 if (!swImage
->Buffer
) {
204 /* probably ran out of memory when allocating tex mem */
209 map
= swImage
->Buffer
;
211 if (texImage
->TexObject
->Target
== GL_TEXTURE_3D
) {
212 GLuint sliceSize
= _mesa_format_image_size(texImage
->TexFormat
,
216 assert(slice
< texImage
->Depth
);
217 map
+= slice
* sliceSize
;
220 /* apply x/y offset to map address */
221 map
+= stride
* (y
/ bh
) + texelSize
* (x
/ bw
);
224 *rowStrideOut
= stride
;
228 _swrast_unmap_teximage(struct gl_context
*ctx
,
229 struct gl_texture_image
*texImage
,
237 _swrast_map_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
239 const GLuint faces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
242 for (face
= 0; face
< faces
; face
++) {
243 for (level
= texObj
->BaseLevel
; level
< MAX_TEXTURE_LEVELS
; level
++) {
244 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
246 struct swrast_texture_image
*swImage
=
247 swrast_texture_image(texImage
);
249 /* XXX we'll eventually call _swrast_map_teximage() here */
250 swImage
->Map
= swImage
->Buffer
;
258 _swrast_unmap_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
260 const GLuint faces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
263 for (face
= 0; face
< faces
; face
++) {
264 for (level
= texObj
->BaseLevel
; level
< MAX_TEXTURE_LEVELS
; level
++) {
265 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
267 struct swrast_texture_image
*swImage
268 = swrast_texture_image(texImage
);
270 /* XXX we'll eventually call _swrast_unmap_teximage() here */
279 * Map all textures for reading prior to software rendering.
282 _swrast_map_textures(struct gl_context
*ctx
)
284 if (ctx
->Texture
._Enabled
) {
285 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
._Current
;
287 _swrast_map_texture(ctx
, texObj
);
293 * Unmap all textures for reading prior to software rendering.
296 _swrast_unmap_textures(struct gl_context
*ctx
)
298 if(ctx
->Texture
._Enabled
) {
299 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
._Current
;
301 _swrast_unmap_texture(ctx
, texObj
);
307 * Called via ctx->Driver.AllocTextureStorage()
308 * Just have to allocate memory for the texture images.
311 _swrast_AllocTextureStorage(struct gl_context
*ctx
,
312 struct gl_texture_object
*texObj
,
313 GLsizei levels
, GLsizei width
,
314 GLsizei height
, GLsizei depth
)
316 const GLint numFaces
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
319 for (face
= 0; face
< numFaces
; face
++) {
320 for (level
= 0; level
< levels
; level
++) {
321 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
322 if (!_swrast_alloc_texture_image_buffer(ctx
, texImage
,