1 /**************************************************************************
3 * Copyright 2006 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 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/internal/p_winsys_screen.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
40 #include "sp_context.h"
42 #include "sp_texture.h"
43 #include "sp_tile_cache.h"
44 #include "sp_screen.h"
47 /* Simple, maximally packed layout.
50 static unsigned minify( unsigned d
)
56 /* Conventional allocation path for non-display textures:
59 softpipe_texture_layout(struct pipe_screen
*screen
,
60 struct softpipe_texture
* spt
)
62 struct pipe_texture
*pt
= &spt
->base
;
64 unsigned width
= pt
->width
[0];
65 unsigned height
= pt
->height
[0];
66 unsigned depth
= pt
->depth
[0];
68 unsigned buffer_size
= 0;
70 for (level
= 0; level
<= pt
->last_level
; level
++) {
71 pt
->width
[level
] = width
;
72 pt
->height
[level
] = height
;
73 pt
->depth
[level
] = depth
;
74 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
75 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
76 spt
->stride
[level
] = pt
->nblocksx
[level
]*pt
->block
.size
;
78 spt
->level_offset
[level
] = buffer_size
;
80 buffer_size
+= (pt
->nblocksy
[level
] *
81 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
84 width
= minify(width
);
85 height
= minify(height
);
86 depth
= minify(depth
);
89 spt
->buffer
= screen
->buffer_create(screen
, 32,
90 PIPE_BUFFER_USAGE_PIXEL
,
93 return spt
->buffer
!= NULL
;
97 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
98 struct softpipe_texture
* spt
)
100 unsigned usage
= (PIPE_BUFFER_USAGE_CPU_READ_WRITE
|
101 PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
103 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
104 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
106 spt
->buffer
= screen
->surface_buffer_create( screen
,
113 return spt
->buffer
!= NULL
;
120 static struct pipe_texture
*
121 softpipe_texture_create(struct pipe_screen
*screen
,
122 const struct pipe_texture
*templat
)
124 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
128 spt
->base
= *templat
;
129 pipe_reference_init(&spt
->base
.reference
, 1);
130 spt
->base
.screen
= screen
;
132 if (spt
->base
.tex_usage
& PIPE_TEXTURE_USAGE_DISPLAY_TARGET
) {
133 if (!softpipe_displaytarget_layout(screen
, spt
))
137 if (!softpipe_texture_layout(screen
, spt
))
141 assert(spt
->base
.reference
.count
== 1);
150 static struct pipe_texture
*
151 softpipe_texture_blanket(struct pipe_screen
* screen
,
152 const struct pipe_texture
*base
,
153 const unsigned *stride
,
154 struct pipe_buffer
*buffer
)
156 struct softpipe_texture
*spt
;
159 /* Only supports one type */
160 if (base
->target
!= PIPE_TEXTURE_2D
||
161 base
->last_level
!= 0 ||
162 base
->depth
[0] != 1) {
166 spt
= CALLOC_STRUCT(softpipe_texture
);
171 pipe_reference_init(&spt
->base
.reference
, 1);
172 spt
->base
.screen
= screen
;
173 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
174 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
175 spt
->stride
[0] = stride
[0];
177 pipe_buffer_reference(&spt
->buffer
, buffer
);
184 softpipe_texture_destroy(struct pipe_texture
*pt
)
186 struct softpipe_texture
*spt
= softpipe_texture(pt
);
188 pipe_buffer_reference(&spt
->buffer
, NULL
);
193 static struct pipe_surface
*
194 softpipe_get_tex_surface(struct pipe_screen
*screen
,
195 struct pipe_texture
*pt
,
196 unsigned face
, unsigned level
, unsigned zslice
,
199 struct softpipe_texture
*spt
= softpipe_texture(pt
);
200 struct pipe_surface
*ps
;
202 assert(level
<= pt
->last_level
);
204 ps
= CALLOC_STRUCT(pipe_surface
);
206 pipe_reference_init(&ps
->reference
, 1);
207 pipe_texture_reference(&ps
->texture
, pt
);
208 ps
->format
= pt
->format
;
209 ps
->width
= pt
->width
[level
];
210 ps
->height
= pt
->height
[level
];
211 ps
->offset
= spt
->level_offset
[level
];
214 /* Because we are softpipe, anything that the state tracker
215 * thought was going to be done with the GPU will actually get
216 * done with the CPU. Let's adjust the flags to take that into
219 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
220 /* GPU_WRITE means "render" and that can involve reads (blending) */
221 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
224 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
225 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
227 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
228 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
229 /* Mark the surface as dirty. The tile cache will look for this. */
230 spt
->modified
= TRUE
;
237 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
238 ps
->offset
+= face
* pt
->nblocksy
[level
] * spt
->stride
[level
];
240 else if (pt
->target
== PIPE_TEXTURE_3D
) {
241 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * spt
->stride
[level
];
253 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
255 /* Effectively do the texture_update work here - if texture images
256 * needed post-processing to put them into hardware layout, this is
257 * where it would happen. For softpipe, nothing to do.
259 assert(surf
->texture
);
260 pipe_texture_reference(&surf
->texture
, NULL
);
265 static struct pipe_transfer
*
266 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
267 struct pipe_texture
*texture
,
268 unsigned face
, unsigned level
, unsigned zslice
,
269 enum pipe_transfer_usage usage
,
270 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
272 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
273 struct softpipe_transfer
*spt
;
276 assert(level
<= texture
->last_level
);
278 spt
= CALLOC_STRUCT(softpipe_transfer
);
280 struct pipe_transfer
*pt
= &spt
->base
;
281 pipe_texture_reference(&pt
->texture
, texture
);
282 pt
->format
= texture
->format
;
283 pt
->block
= texture
->block
;
288 pt
->nblocksx
= texture
->nblocksx
[level
];
289 pt
->nblocksy
= texture
->nblocksy
[level
];
290 pt
->stride
= sptex
->stride
[level
];
296 spt
->offset
= sptex
->level_offset
[level
];
298 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
299 spt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
301 else if (texture
->target
== PIPE_TEXTURE_3D
) {
302 spt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
315 softpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
317 /* Effectively do the texture_update work here - if texture images
318 * needed post-processing to put them into hardware layout, this is
319 * where it would happen. For softpipe, nothing to do.
321 assert (transfer
->texture
);
322 pipe_texture_reference(&transfer
->texture
, NULL
);
328 softpipe_transfer_map( struct pipe_screen
*screen
,
329 struct pipe_transfer
*transfer
)
332 struct softpipe_texture
*spt
;
335 assert(transfer
->texture
);
336 spt
= softpipe_texture(transfer
->texture
);
338 if (transfer
->usage
!= PIPE_TRANSFER_READ
) {
339 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
342 if (transfer
->usage
!= PIPE_TRANSFER_WRITE
) {
343 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
346 map
= pipe_buffer_map(screen
, spt
->buffer
, flags
);
350 /* May want to different things here depending on read/write nature
353 if (transfer
->texture
&& transfer
->usage
!= PIPE_TRANSFER_READ
)
355 /* Do something to notify sharing contexts of a texture change.
356 * In softpipe, that would mean flushing the texture cache.
358 softpipe_screen(screen
)->timestamp
++;
361 return map
+ softpipe_transfer(transfer
)->offset
+
362 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
363 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
368 softpipe_transfer_unmap(struct pipe_screen
*screen
,
369 struct pipe_transfer
*transfer
)
371 struct softpipe_texture
*spt
;
373 assert(transfer
->texture
);
374 spt
= softpipe_texture(transfer
->texture
);
376 pipe_buffer_unmap( screen
, spt
->buffer
);
381 softpipe_init_texture_funcs(struct softpipe_context
*sp
)
387 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
389 screen
->texture_create
= softpipe_texture_create
;
390 screen
->texture_blanket
= softpipe_texture_blanket
;
391 screen
->texture_destroy
= softpipe_texture_destroy
;
393 screen
->get_tex_surface
= softpipe_get_tex_surface
;
394 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;
396 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
397 screen
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
398 screen
->transfer_map
= softpipe_transfer_map
;
399 screen
->transfer_unmap
= softpipe_transfer_unmap
;