2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "r300_texture.h"
25 static int minify(int i
)
27 return MAX2(1, i
>> 1);
30 static void r300_setup_miptree(struct r300_texture
* tex
)
32 struct pipe_texture
* base
= &tex
->tex
;
33 int stride
, size
, offset
;
36 for (i
= 0; i
<= base
->last_level
; i
++) {
38 base
->width
[i
] = minify(base
->width
[i
-1]);
39 base
->height
[i
] = minify(base
->height
[i
-1]);
40 base
->depth
[i
] = minify(base
->depth
[i
-1]);
43 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
44 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->width
[i
]);
46 /* Radeons enjoy things in multiples of 32. */
47 /* XXX NPOT -> 64, not 32 */
48 stride
= (base
->nblocksx
[i
] * base
->block
.size
+ 63) & ~63;
49 size
= stride
* base
->nblocksy
[i
] * base
->depth
[i
];
52 tex
->offset
[i
] = (tex
->size
+ 63) & ~63;
53 tex
->size
= tex
->offset
[i
] + size
;
57 /* Create a new texture. */
58 static struct pipe_texture
*
59 r300_texture_create(struct pipe_screen
* screen
,
60 const struct pipe_texture
* template)
62 /* XXX struct r300_screen* r300screen = r300_screen(screen); */
64 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
71 tex
->tex
.refcount
= 1;
72 tex
->tex
.screen
= screen
;
74 r300_setup_miptree(tex
);
76 tex
->buffer
= screen
->buffer_create(screen
, 64,
77 PIPE_BUFFER_USAGE_PIXEL
,
85 return (struct pipe_texture
*)tex
;
88 static void r300_texture_release(struct pipe_screen
* screen
,
89 struct pipe_texture
** texture
)
95 (*texture
)->refcount
--;
97 if ((*texture
)->refcount
<= 0) {
98 struct r300_texture
* tex
= (struct r300_texture
*)*texture
;
100 pipe_buffer_reference(screen
, &tex
->buffer
, NULL
);
108 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
109 struct pipe_texture
* texture
,
115 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
116 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
119 /* XXX this is certainly dependent on tex target */
120 offset
= tex
->offset
[level
];
123 surface
->refcount
= 1;
124 pipe_texture_reference(&surface
->texture
, texture
);
125 surface
->format
= texture
->format
;
126 surface
->width
= texture
->width
[level
];
127 surface
->height
= texture
->height
[level
];
128 surface
->offset
= offset
;
129 surface
->usage
= flags
;
130 surface
->status
= PIPE_SURFACE_STATUS_DEFINED
;
136 static void r300_tex_surface_release(struct pipe_screen
* screen
,
137 struct pipe_surface
** surface
)
139 struct pipe_surface
* s
= *surface
;
143 if (s
->refcount
<= 0) {
144 pipe_texture_reference(&s
->texture
, NULL
);
151 static struct pipe_texture
*
152 r300_texture_blanket(struct pipe_screen
* screen
,
153 const struct pipe_texture
* base
,
154 const unsigned* stride
,
155 struct pipe_buffer
* buffer
)
157 struct r300_texture
* tex
;
159 if (base
->target
!= PIPE_TEXTURE_2D
||
160 base
->last_level
!= 0 ||
161 base
->depth
[0] != 1) {
165 tex
= CALLOC_STRUCT(r300_texture
);
171 tex
->tex
.refcount
= 1;
172 tex
->tex
.screen
= screen
;
174 tex
->stride
= *stride
;
176 pipe_buffer_reference(screen
, &tex
->buffer
, buffer
);
178 return (struct pipe_texture
*)tex
;
181 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
183 screen
->texture_create
= r300_texture_create
;
184 screen
->texture_release
= r300_texture_release
;
185 screen
->get_tex_surface
= r300_get_tex_surface
;
186 screen
->tex_surface_release
= r300_tex_surface_release
;
187 screen
->texture_blanket
= r300_texture_blanket
;