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/fbobject.h"
31 #include "main/teximage.h"
32 #include "swrast/swrast.h"
33 #include "swrast/s_context.h"
37 * Allocate a new swrast_texture_image (a subclass of gl_texture_image).
38 * Called via ctx->Driver.NewTextureImage().
40 struct gl_texture_image
*
41 _swrast_new_texture_image( struct gl_context
*ctx
)
44 return (struct gl_texture_image
*) CALLOC_STRUCT(swrast_texture_image
);
49 * Free a swrast_texture_image (a subclass of gl_texture_image).
50 * Called via ctx->Driver.DeleteTextureImage().
53 _swrast_delete_texture_image(struct gl_context
*ctx
,
54 struct gl_texture_image
*texImage
)
56 /* Nothing special for the subclass yet */
57 _mesa_delete_texture_image(ctx
, texImage
);
62 * Called via ctx->Driver.AllocTextureImageBuffer()
65 _swrast_alloc_texture_image_buffer(struct gl_context
*ctx
,
66 struct gl_texture_image
*texImage
,
67 gl_format format
, GLsizei width
,
68 GLsizei height
, GLsizei depth
)
70 struct swrast_texture_image
*swImg
= swrast_texture_image(texImage
);
71 GLuint bytes
= _mesa_format_image_size(format
, width
, height
, depth
);
74 /* This _should_ be true (revisit if these ever fail) */
75 assert(texImage
->Width
== width
);
76 assert(texImage
->Height
== height
);
77 assert(texImage
->Depth
== depth
);
79 assert(!swImg
->Buffer
);
80 swImg
->Buffer
= _mesa_align_malloc(bytes
, 512);
84 /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */
85 swImg
->RowStride
= width
;
87 /* Allocate the ImageOffsets array and initialize to typical values.
88 * We allocate the array for 1D/2D textures too in order to avoid special-
89 * case code in the texstore routines.
91 swImg
->ImageOffsets
= (GLuint
*) malloc(depth
* sizeof(GLuint
));
92 if (!swImg
->ImageOffsets
)
95 for (i
= 0; i
< depth
; i
++) {
96 swImg
->ImageOffsets
[i
] = i
* width
* height
;
99 if ((width
== 1 || _mesa_is_pow_two(texImage
->Width2
)) &&
100 (height
== 1 || _mesa_is_pow_two(texImage
->Height2
)) &&
101 (depth
== 1 || _mesa_is_pow_two(texImage
->Depth2
)))
102 swImg
->_IsPowerOfTwo
= GL_TRUE
;
104 swImg
->_IsPowerOfTwo
= GL_FALSE
;
106 /* Compute Width/Height/DepthScale for mipmap lod computation */
107 if (texImage
->TexObject
->Target
== GL_TEXTURE_RECTANGLE_NV
) {
108 /* scale = 1.0 since texture coords directly map to texels */
109 swImg
->WidthScale
= 1.0;
110 swImg
->HeightScale
= 1.0;
111 swImg
->DepthScale
= 1.0;
114 swImg
->WidthScale
= (GLfloat
) texImage
->Width
;
115 swImg
->HeightScale
= (GLfloat
) texImage
->Height
;
116 swImg
->DepthScale
= (GLfloat
) texImage
->Depth
;
124 * Called via ctx->Driver.FreeTextureImageBuffer()
127 _swrast_free_texture_image_buffer(struct gl_context
*ctx
,
128 struct gl_texture_image
*texImage
)
130 struct swrast_texture_image
*swImage
= swrast_texture_image(texImage
);
131 if (swImage
->Buffer
) {
132 _mesa_align_free(swImage
->Buffer
);
133 swImage
->Buffer
= NULL
;
136 if (swImage
->ImageOffsets
) {
137 free(swImage
->ImageOffsets
);
138 swImage
->ImageOffsets
= NULL
;
144 * Error checking for debugging only.
147 _mesa_check_map_teximage(struct gl_texture_image
*texImage
,
148 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
)
151 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D
)
152 assert(y
== 0 && h
== 1);
154 assert(x
< texImage
->Width
|| texImage
->Width
== 0);
155 assert(y
< texImage
->Height
|| texImage
->Height
== 0);
156 assert(x
+ w
<= texImage
->Width
);
157 assert(y
+ h
<= texImage
->Height
);
161 * Map a 2D slice of a texture image into user space.
162 * (x,y,w,h) defines a region of interest (ROI). Reading/writing texels
163 * outside of the ROI is undefined.
165 * \param texImage the texture image
166 * \param slice the 3D image slice or array texture slice
167 * \param x, y, w, h region of interest
168 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
169 * \param mapOut returns start of mapping of region of interest
170 * \param rowStrideOut returns row stride (in bytes)
173 _swrast_map_teximage(struct gl_context
*ctx
,
174 struct gl_texture_image
*texImage
,
176 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
181 struct swrast_texture_image
*swImage
= swrast_texture_image(texImage
);
183 GLint stride
, texelSize
;
186 _mesa_check_map_teximage(texImage
, slice
, x
, y
, w
, h
);
188 texelSize
= _mesa_get_format_bytes(texImage
->TexFormat
);
189 stride
= _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
190 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
192 assert(swImage
->Buffer
);
194 map
= swImage
->Buffer
;
196 if (texImage
->TexObject
->Target
== GL_TEXTURE_3D
||
197 texImage
->TexObject
->Target
== GL_TEXTURE_2D_ARRAY
) {
198 GLuint sliceSize
= _mesa_format_image_size(texImage
->TexFormat
,
202 assert(slice
< texImage
->Depth
);
203 map
+= slice
* sliceSize
;
204 } else if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
205 GLuint sliceSize
= _mesa_format_image_size(texImage
->TexFormat
,
209 assert(slice
< texImage
->Height
);
210 map
+= slice
* sliceSize
;
213 /* apply x/y offset to map address */
214 map
+= stride
* (y
/ bh
) + texelSize
* (x
/ bw
);
217 *rowStrideOut
= stride
;
221 _swrast_unmap_teximage(struct gl_context
*ctx
,
222 struct gl_texture_image
*texImage
,
230 _swrast_map_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
232 const GLuint faces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
235 for (face
= 0; face
< faces
; face
++) {
236 for (level
= texObj
->BaseLevel
; level
< MAX_TEXTURE_LEVELS
; level
++) {
237 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
239 struct swrast_texture_image
*swImage
=
240 swrast_texture_image(texImage
);
242 /* XXX we'll eventually call _swrast_map_teximage() here */
243 swImage
->Data
= swImage
->Buffer
;
251 _swrast_unmap_texture(struct gl_context
*ctx
, struct gl_texture_object
*texObj
)
253 const GLuint faces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
256 for (face
= 0; face
< faces
; face
++) {
257 for (level
= texObj
->BaseLevel
; level
< MAX_TEXTURE_LEVELS
; level
++) {
258 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
260 struct swrast_texture_image
*swImage
261 = swrast_texture_image(texImage
);
263 /* XXX we'll eventually call _swrast_unmap_teximage() here */
264 swImage
->Data
= NULL
;
272 * Map all textures for reading prior to software rendering.
275 _swrast_map_textures(struct gl_context
*ctx
)
277 GLbitfield enabledUnits
= ctx
->Texture
._EnabledUnits
;
279 /* loop over enabled texture units */
280 while (enabledUnits
) {
281 GLuint unit
= _mesa_ffs(enabledUnits
) - 1;
282 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
284 _swrast_map_texture(ctx
, texObj
);
286 enabledUnits
&= ~(1 << unit
);
292 * Unmap all textures for reading prior to software rendering.
295 _swrast_unmap_textures(struct gl_context
*ctx
)
297 GLbitfield enabledUnits
= ctx
->Texture
._EnabledUnits
;
299 /* loop over enabled texture units */
300 while (enabledUnits
) {
301 GLuint unit
= _mesa_ffs(enabledUnits
) - 1;
302 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
304 _swrast_unmap_texture(ctx
, texObj
);
306 enabledUnits
&= ~(1 << unit
);
312 * Map or unmap any textures that we may be rendering to as renderbuffers.
315 map_unmap_renderbuffers(struct gl_context
*ctx
,
316 struct gl_framebuffer
*fb
,
321 for (i
= 0; i
< Elements(fb
->Attachment
); i
++) {
322 struct gl_texture_object
*texObj
= fb
->Attachment
[i
].Texture
;
324 const GLuint level
= fb
->Attachment
[i
].TextureLevel
;
325 const GLuint face
= fb
->Attachment
[i
].CubeMapFace
;
326 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
328 struct swrast_texture_image
*swImage
329 = swrast_texture_image(texImage
);
332 /* XXX we'll eventually call _swrast_map_teximage() here */
333 swImage
->Data
= swImage
->Buffer
;
336 /* XXX we'll eventually call _swrast_unmap_teximage() here */
337 swImage
->Data
= NULL
;
346 _swrast_map_renderbuffers(struct gl_context
*ctx
)
348 map_unmap_renderbuffers(ctx
, ctx
->DrawBuffer
, GL_TRUE
);
349 if (ctx
->ReadBuffer
!= ctx
->DrawBuffer
)
350 map_unmap_renderbuffers(ctx
, ctx
->ReadBuffer
, GL_TRUE
);
355 _swrast_unmap_renderbuffers(struct gl_context
*ctx
)
357 map_unmap_renderbuffers(ctx
, ctx
->DrawBuffer
, GL_FALSE
);
358 if (ctx
->ReadBuffer
!= ctx
->DrawBuffer
)
359 map_unmap_renderbuffers(ctx
, ctx
->ReadBuffer
, GL_FALSE
);
365 * Called via ctx->Driver.AllocTextureStorage()
366 * Just have to allocate memory for the texture images.
369 _swrast_AllocTextureStorage(struct gl_context
*ctx
,
370 struct gl_texture_object
*texObj
,
371 GLsizei levels
, GLsizei width
,
372 GLsizei height
, GLsizei depth
)
374 const GLint numFaces
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
377 for (face
= 0; face
< numFaces
; face
++) {
378 for (level
= 0; level
< levels
; level
++) {
379 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
380 if (!_swrast_alloc_texture_image_buffer(ctx
, texImage
,