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