swrast: introduce new swrast_texture_image struct
[mesa.git] / src / mesa / swrast / s_texture.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2011 VMware, Inc.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
22 */
23
24 /**
25 * Functions for mapping/unmapping texture images.
26 */
27
28
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"
34
35
36 /**
37 * Allocate a new swrast_texture_image (a subclass of gl_texture_image).
38 * Called via ctx->Driver.NewTextureImage().
39 */
40 struct gl_texture_image *
41 _swrast_new_texture_image( struct gl_context *ctx )
42 {
43 (void) ctx;
44 return (struct gl_texture_image *) CALLOC_STRUCT(swrast_texture_image);
45 }
46
47
48 /**
49 * Free a swrast_texture_image (a subclass of gl_texture_image).
50 * Called via ctx->Driver.DeleteTextureImage().
51 */
52 void
53 _swrast_delete_texture_image(struct gl_context *ctx,
54 struct gl_texture_image *texImage)
55 {
56 /* Nothing special for the subclass yet */
57 _mesa_delete_texture_image(ctx, texImage);
58 }
59
60
61 /**
62 * Error checking for debugging only.
63 */
64 static void
65 _mesa_check_map_teximage(struct gl_texture_image *texImage,
66 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h)
67 {
68
69 if (texImage->TexObject->Target == GL_TEXTURE_1D)
70 assert(y == 0 && h == 1);
71
72 assert(x < texImage->Width || texImage->Width == 0);
73 assert(y < texImage->Height || texImage->Height == 0);
74 assert(x + w <= texImage->Width);
75 assert(y + h <= texImage->Height);
76 }
77
78 /**
79 * Map a 2D slice of a texture image into user space.
80 * (x,y,w,h) defines a region of interest (ROI). Reading/writing texels
81 * outside of the ROI is undefined.
82 *
83 * \param texImage the texture image
84 * \param slice the 3D image slice or array texture slice
85 * \param x, y, w, h region of interest
86 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
87 * \param mapOut returns start of mapping of region of interest
88 * \param rowStrideOut returns row stride (in bytes)
89 */
90 void
91 _swrast_map_teximage(struct gl_context *ctx,
92 struct gl_texture_image *texImage,
93 GLuint slice,
94 GLuint x, GLuint y, GLuint w, GLuint h,
95 GLbitfield mode,
96 GLubyte **mapOut,
97 GLint *rowStrideOut)
98 {
99 GLubyte *map;
100 GLint stride, texelSize;
101 GLuint bw, bh;
102
103 _mesa_check_map_teximage(texImage, slice, x, y, w, h);
104
105 texelSize = _mesa_get_format_bytes(texImage->TexFormat);
106 stride = _mesa_format_row_stride(texImage->TexFormat, texImage->Width);
107 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
108
109 assert(texImage->Data);
110
111 map = texImage->Data;
112
113 if (texImage->TexObject->Target == GL_TEXTURE_3D ||
114 texImage->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
115 GLuint sliceSize = _mesa_format_image_size(texImage->TexFormat,
116 texImage->Width,
117 texImage->Height,
118 1);
119 assert(slice < texImage->Depth);
120 map += slice * sliceSize;
121 }
122
123 /* apply x/y offset to map address */
124 map += stride * (y / bh) + texelSize * (x / bw);
125
126 *mapOut = map;
127 *rowStrideOut = stride;
128 }
129
130 void
131 _swrast_unmap_teximage(struct gl_context *ctx,
132 struct gl_texture_image *texImage,
133 GLuint slice)
134 {
135 /* nop */
136 }