1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "st_context.h"
29 #include "st_format.h"
30 #include "st_public.h"
31 #include "st_texture.h"
32 #include "st_cb_fbo.h"
33 #include "main/enums.h"
34 #include "main/teximage.h"
36 #undef Elements /* fix re-defined macro warning */
38 #include "pipe/p_state.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_inlines.h"
42 #include "pipe/p_inlines.h"
43 #include "util/u_rect.h"
46 #define DBG if(0) printf
50 target_to_target(GLenum target
)
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
55 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
56 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
57 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
58 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
59 return GL_TEXTURE_CUBE_MAP_ARB
;
68 * Allocate a new pipe_texture object
69 * width0, height0, depth0 are the dimensions of the level 0 image
70 * (the highest resolution). last_level indicates how many mipmap levels
71 * to allocate storage for. For non-mipmapped textures, this will be zero.
74 st_texture_create(struct st_context
*st
,
75 enum pipe_texture_target target
,
76 enum pipe_format format
,
84 struct pipe_texture pt
, *newtex
;
85 struct pipe_screen
*screen
= st
->pipe
->screen
;
87 assert(target
<= PIPE_TEXTURE_CUBE
);
89 DBG("%s target %s format %s last_level %d\n", __FUNCTION__
,
90 _mesa_lookup_enum_by_nr(target
),
91 _mesa_lookup_enum_by_nr(format
), last_level
);
94 assert(screen
->is_format_supported(screen
, format
, target
,
95 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
97 memset(&pt
, 0, sizeof(pt
));
100 pt
.last_level
= last_level
;
101 pt
.width
[0] = width0
;
102 pt
.height
[0] = height0
;
103 pt
.depth
[0] = depth0
;
104 pt
.compressed
= compress_byte
? 1 : 0;
105 pf_get_block(format
, &pt
.block
);
106 pt
.tex_usage
= usage
;
108 newtex
= screen
->texture_create(screen
, &pt
);
110 assert(!newtex
|| newtex
->refcount
== 1);
117 * Check if a texture image be pulled into a unified mipmap texture.
118 * This mirrors the completeness test in a lot of ways.
120 * Not sure whether I want to pass gl_texture_image here.
123 st_texture_match_image(const struct pipe_texture
*pt
,
124 const struct gl_texture_image
*image
,
125 GLuint face
, GLuint level
)
127 /* Images with borders are never pulled into mipmap textures.
132 if (st_mesa_format_to_pipe_format(image
->TexFormat
->MesaFormat
) != pt
->format
||
133 image
->IsCompressed
!= pt
->compressed
)
136 /* Test image dimensions against the base level image adjusted for
137 * minification. This will also catch images not present in the
138 * texture, changed targets, etc.
140 if (image
->Width
!= pt
->width
[level
] ||
141 image
->Height
!= pt
->height
[level
] ||
142 image
->Depth
!= pt
->depth
[level
])
150 /* Although we use the image_offset[] array to store relative offsets
151 * to cube faces, Mesa doesn't know anything about this and expects
152 * each cube face to be treated as a separate image.
154 * These functions present that view to mesa:
157 st_texture_depth_offsets(struct pipe_texture
*pt
, GLuint level
)
159 static const GLuint zero
= 0;
161 if (pt
->target
!= PIPE_TEXTURE_3D
|| pt
->level
[level
].nr_images
== 1)
164 return pt
->level
[level
].image_offset
;
169 * Return the offset to the given mipmap texture image within the
170 * texture memory buffer, in bytes.
173 st_texture_image_offset(const struct pipe_texture
* pt
,
174 GLuint face
, GLuint level
)
176 if (pt
->target
== PIPE_TEXTURE_CUBE
)
177 return (pt
->level
[level
].level_offset
+
178 pt
->level
[level
].image_offset
[face
] * pt
->cpp
);
180 return pt
->level
[level
].level_offset
;
186 * Map a teximage in a mipmap texture.
187 * \param row_stride returns row stride in bytes
188 * \param image_stride returns image stride in bytes (for 3D textures).
189 * \return address of mapping
192 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
196 struct pipe_screen
*screen
= st
->pipe
->screen
;
197 struct pipe_texture
*pt
= stImage
->pt
;
198 DBG("%s \n", __FUNCTION__
);
200 stImage
->surface
= screen
->get_tex_surface(screen
, pt
, stImage
->face
,
201 stImage
->level
, zoffset
,
204 if (stImage
->surface
)
205 return screen
->surface_map(screen
, stImage
->surface
, flags
);
212 st_texture_image_unmap(struct st_context
*st
,
213 struct st_texture_image
*stImage
)
215 struct pipe_screen
*screen
= st
->pipe
->screen
;
217 DBG("%s\n", __FUNCTION__
);
219 screen
->surface_unmap(screen
, stImage
->surface
);
221 pipe_surface_reference(&stImage
->surface
, NULL
);
227 * Upload data to a rectangular sub-region. Lots of choices how to do this:
229 * - memcpy by span to current destination
230 * - upload data as new buffer and blit
232 * Currently always memcpy.
235 st_surface_data(struct pipe_context
*pipe
,
236 struct pipe_surface
*dst
,
237 unsigned dstx
, unsigned dsty
,
238 const void *src
, unsigned src_stride
,
239 unsigned srcx
, unsigned srcy
, unsigned width
, unsigned height
)
241 struct pipe_screen
*screen
= pipe
->screen
;
242 void *map
= screen
->surface_map(screen
, dst
, PIPE_BUFFER_USAGE_CPU_WRITE
);
252 screen
->surface_unmap(screen
, dst
);
256 /* Upload data for a particular image.
259 st_texture_image_data(struct pipe_context
*pipe
,
260 struct pipe_texture
*dst
,
264 GLuint src_row_stride
, GLuint src_image_stride
)
266 struct pipe_screen
*screen
= pipe
->screen
;
267 GLuint depth
= dst
->depth
[level
];
269 const GLubyte
*srcUB
= src
;
270 struct pipe_surface
*dst_surface
;
272 DBG("%s\n", __FUNCTION__
);
273 for (i
= 0; i
< depth
; i
++) {
274 dst_surface
= screen
->get_tex_surface(screen
, dst
, face
, level
, i
,
275 PIPE_BUFFER_USAGE_CPU_WRITE
);
277 st_surface_data(pipe
, dst_surface
,
278 0, 0, /* dstx, dsty */
281 0, 0, /* source x, y */
282 dst
->width
[level
], dst
->height
[level
]); /* width, height */
284 screen
->tex_surface_release(screen
, &dst_surface
);
286 srcUB
+= src_image_stride
;
291 /* Copy mipmap image between textures
294 st_texture_image_copy(struct pipe_context
*pipe
,
295 struct pipe_texture
*dst
, GLuint dstLevel
,
296 struct pipe_texture
*src
,
299 struct pipe_screen
*screen
= pipe
->screen
;
300 GLuint width
= dst
->width
[dstLevel
];
301 GLuint height
= dst
->height
[dstLevel
];
302 GLuint depth
= dst
->depth
[dstLevel
];
303 struct pipe_surface
*src_surface
;
304 struct pipe_surface
*dst_surface
;
307 /* XXX this is a hack */
308 const GLuint copyHeight
= dst
->compressed
? height
/ 4 : height
;
310 for (i
= 0; i
< depth
; i
++) {
313 /* find src texture level of needed size */
314 for (srcLevel
= 0; srcLevel
<= src
->last_level
; srcLevel
++) {
315 if (src
->width
[srcLevel
] == width
&&
316 src
->height
[srcLevel
] == height
) {
320 assert(src
->width
[srcLevel
] == width
);
321 assert(src
->height
[srcLevel
] == height
);
325 src_surface
= screen
->get_tex_surface(screen
, src
, face
, srcLevel
, i
,
326 PIPE_BUFFER_USAGE_CPU_READ
);
327 ubyte
*map
= screen
->surface_map(screen
, src_surface
, PIPE_BUFFER_USAGE_CPU_READ
);
328 map
+= src_surface
->width
* src_surface
->height
* 4 / 2;
329 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
331 map
[0], map
[1], map
[2], map
[3],
332 src
, srcLevel
, dst
, dstLevel
);
334 screen
->surface_unmap(screen
, src_surface
);
335 pipe_surface_reference(&src_surface
, NULL
);
339 dst_surface
= screen
->get_tex_surface(screen
, dst
, face
, dstLevel
, i
,
340 PIPE_BUFFER_USAGE_GPU_WRITE
);
342 src_surface
= screen
->get_tex_surface(screen
, src
, face
, srcLevel
, i
,
343 PIPE_BUFFER_USAGE_GPU_READ
);
345 pipe
->surface_copy(pipe
,
353 screen
->tex_surface_release(screen
, &src_surface
);
354 screen
->tex_surface_release(screen
, &dst_surface
);
359 /** Redirect rendering into stfb's surface to a texture image */
361 st_bind_teximage(struct st_framebuffer
*stfb
, uint surfIndex
,
362 int target
, int format
, int level
)
364 GET_CURRENT_CONTEXT(ctx
);
365 struct st_context
*st
= ctx
->st
;
366 struct pipe_context
*pipe
= st
->pipe
;
367 struct pipe_screen
*screen
= pipe
->screen
;
368 const GLuint unit
= ctx
->Texture
.CurrentUnit
;
369 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
370 struct gl_texture_object
*texObj
;
371 struct gl_texture_image
*texImage
;
372 struct st_texture_image
*stImage
;
373 struct st_renderbuffer
*strb
;
374 GLint face
= 0, slice
= 0;
376 assert(surfIndex
<= ST_SURFACE_DEPTH
);
378 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
380 if (strb
->texture_save
|| strb
->surface_save
) {
385 if (target
== ST_TEXTURE_2D
) {
386 texObj
= texUnit
->Current2D
;
387 texImage
= _mesa_get_tex_image(ctx
, texObj
, GL_TEXTURE_2D
, level
);
388 stImage
= st_texture_image(texImage
);
391 /* unsupported target */
395 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
397 /* save the renderbuffer's surface/texture info */
398 pipe_texture_reference(&strb
->texture_save
, strb
->texture
);
399 pipe_surface_reference(&strb
->surface_save
, strb
->surface
);
401 /* plug in new surface/texture info */
402 pipe_texture_reference(&strb
->texture
, stImage
->pt
);
403 strb
->surface
= screen
->get_tex_surface(screen
, strb
->texture
,
405 (PIPE_BUFFER_USAGE_GPU_READ
|
406 PIPE_BUFFER_USAGE_GPU_WRITE
));
408 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
414 /** Undo surface-to-texture binding */
416 st_release_teximage(struct st_framebuffer
*stfb
, uint surfIndex
,
417 int target
, int format
, int level
)
419 GET_CURRENT_CONTEXT(ctx
);
420 struct st_context
*st
= ctx
->st
;
421 struct st_renderbuffer
*strb
;
423 assert(surfIndex
<= ST_SURFACE_DEPTH
);
425 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
427 if (!strb
->texture_save
|| !strb
->surface_save
) {
432 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
434 /* free tex surface, restore original */
435 pipe_surface_reference(&strb
->surface
, strb
->surface_save
);
436 pipe_texture_reference(&strb
->texture
, strb
->texture_save
);
438 pipe_surface_reference(&strb
->surface_save
, NULL
);
439 pipe_texture_reference(&strb
->texture_save
, NULL
);
441 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;