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