1#include "swrast/swrast.h"
2#include "main/renderbuffer.h"
3#include "main/texobj.h"
4#include "main/teximage.h"
5#include "main/mipmap.h"
6#include "drivers/common/meta.h"
7#include "intel_context.h"
8#include "intel_mipmap_tree.h"
9#include "intel_tex.h"
10
11#define FILE_DEBUG_FLAG DEBUG_TEXTURE
12
13static struct gl_texture_image *
14intelNewTextureImage(struct gl_context * ctx)
15{
16   DBG("%s\n", __FUNCTION__);
17   (void) ctx;
18   return (struct gl_texture_image *) CALLOC_STRUCT(intel_texture_image);
19}
20
21static void
22intelDeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
23{
24   /* nothing special (yet) for intel_texture_image */
25   _mesa_delete_texture_image(ctx, img);
26}
27
28
29static struct gl_texture_object *
30intelNewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
31{
32   struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
33
34   (void) ctx;
35
36   DBG("%s\n", __FUNCTION__);
37   _mesa_initialize_texture_object(&obj->base, name, target);
38
39   return &obj->base;
40}
41
42static void
43intelDeleteTextureObject(struct gl_context *ctx,
44			 struct gl_texture_object *texObj)
45{
46   struct intel_texture_object *intelObj = intel_texture_object(texObj);
47
48   intel_miptree_release(&intelObj->mt);
49   _mesa_delete_texture_object(ctx, texObj);
50}
51
52static GLboolean
53intel_alloc_texture_image_buffer(struct gl_context *ctx,
54				 struct gl_texture_image *image)
55{
56   struct intel_context *intel = intel_context(ctx);
57   struct intel_texture_image *intel_image = intel_texture_image(image);
58   struct gl_texture_object *texobj = image->TexObject;
59   struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
60   GLuint slices;
61
62   assert(image->Border == 0);
63
64   /* Because the driver uses AllocTextureImageBuffer() internally, it may end
65    * up mismatched with FreeTextureImageBuffer(), but that is safe to call
66    * multiple times.
67    */
68   ctx->Driver.FreeTextureImageBuffer(ctx, image);
69
70   /* Allocate the swrast_texture_image::ImageOffsets array now */
71   switch (texobj->Target) {
72   case GL_TEXTURE_3D:
73   case GL_TEXTURE_2D_ARRAY:
74      slices = image->Depth;
75      break;
76   case GL_TEXTURE_1D_ARRAY:
77      slices = image->Height;
78      break;
79   default:
80      slices = 1;
81   }
82   assert(!intel_image->base.ImageOffsets);
83   intel_image->base.ImageOffsets = malloc(slices * sizeof(GLuint));
84
85   _swrast_init_texture_image(image);
86
87   if (intel_texobj->mt &&
88       intel_miptree_match_image(intel_texobj->mt, image)) {
89      intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
90      DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
91          __FUNCTION__, texobj, image->Level,
92          image->Width, image->Height, image->Depth, intel_texobj->mt);
93   } else {
94      intel_image->mt = intel_miptree_create_for_teximage(intel, intel_texobj,
95                                                          intel_image,
96                                                          false);
97
98      /* Even if the object currently has a mipmap tree associated
99       * with it, this one is a more likely candidate to represent the
100       * whole object since our level didn't fit what was there
101       * before, and any lower levels would fit into our miptree.
102       */
103      intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
104
105      DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
106          __FUNCTION__, texobj, image->Level,
107          image->Width, image->Height, image->Depth, intel_image->mt);
108   }
109
110   return true;
111}
112
113/**
114 * Called via ctx->Driver.AllocTextureStorage()
115 * Just have to allocate memory for the texture images.
116 */
117static GLboolean
118intel_alloc_texture_storage(struct gl_context *ctx,
119                            struct gl_texture_object *texObj,
120                            GLsizei levels, GLsizei width,
121                            GLsizei height, GLsizei depth)
122{
123   const int numFaces = _mesa_num_tex_faces(texObj->Target);
124   int face;
125   int level;
126
127   for (face = 0; face < numFaces; face++) {
128      for (level = 0; level < levels; level++) {
129         struct gl_texture_image *const texImage = texObj->Image[face][level];
130         if (!intel_alloc_texture_image_buffer(ctx, texImage))
131            return false;
132      }
133   }
134
135   return true;
136}
137
138static void
139intel_free_texture_image_buffer(struct gl_context * ctx,
140				struct gl_texture_image *texImage)
141{
142   struct intel_texture_image *intelImage = intel_texture_image(texImage);
143
144   DBG("%s\n", __FUNCTION__);
145
146   intel_miptree_release(&intelImage->mt);
147
148   if (intelImage->base.Buffer) {
149      _mesa_align_free(intelImage->base.Buffer);
150      intelImage->base.Buffer = NULL;
151   }
152
153   if (intelImage->base.ImageOffsets) {
154      free(intelImage->base.ImageOffsets);
155      intelImage->base.ImageOffsets = NULL;
156   }
157}
158
159/**
160 * Map texture memory/buffer into user space.
161 * Note: the region of interest parameters are ignored here.
162 * \param mode  bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
163 * \param mapOut  returns start of mapping of region of interest
164 * \param rowStrideOut  returns row stride in bytes
165 */
166static void
167intel_map_texture_image(struct gl_context *ctx,
168			struct gl_texture_image *tex_image,
169			GLuint slice,
170			GLuint x, GLuint y, GLuint w, GLuint h,
171			GLbitfield mode,
172			GLubyte **map,
173			GLint *stride)
174{
175   struct intel_context *intel = intel_context(ctx);
176   struct intel_texture_image *intel_image = intel_texture_image(tex_image);
177   struct intel_mipmap_tree *mt = intel_image->mt;
178
179   /* Our texture data is always stored in a miptree. */
180   assert(mt);
181
182   /* Check that our caller wasn't confused about how to map a 1D texture. */
183   assert(tex_image->TexObject->Target != GL_TEXTURE_1D_ARRAY ||
184	  h == 1);
185
186   /* intel_miptree_map operates on a unified "slice" number that references the
187    * cube face, since it's all just slices to the miptree code.
188    */
189   if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
190      slice = tex_image->Face;
191
192   intel_miptree_map(intel, mt, tex_image->Level, slice, x, y, w, h, mode,
193		     (void **)map, stride);
194}
195
196static void
197intel_unmap_texture_image(struct gl_context *ctx,
198			  struct gl_texture_image *tex_image, GLuint slice)
199{
200   struct intel_context *intel = intel_context(ctx);
201   struct intel_texture_image *intel_image = intel_texture_image(tex_image);
202   struct intel_mipmap_tree *mt = intel_image->mt;
203
204   if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
205      slice = tex_image->Face;
206
207   intel_miptree_unmap(intel, mt, tex_image->Level, slice);
208}
209
210void
211intelInitTextureFuncs(struct dd_function_table *functions)
212{
213   functions->NewTextureObject = intelNewTextureObject;
214   functions->NewTextureImage = intelNewTextureImage;
215   functions->DeleteTextureImage = intelDeleteTextureImage;
216   functions->DeleteTexture = intelDeleteTextureObject;
217   functions->AllocTextureImageBuffer = intel_alloc_texture_image_buffer;
218   functions->FreeTextureImageBuffer = intel_free_texture_image_buffer;
219   functions->AllocTextureStorage = intel_alloc_texture_storage;
220   functions->MapTextureImage = intel_map_texture_image;
221   functions->UnmapTextureImage = intel_unmap_texture_image;
222}
223