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 "pipe/p_screen.h"
25 #include "util/u_math.h"
26 #include "util/u_memory.h"
28 #include "r300_context.h"
29 #include "r300_texture.h"
31 static void r300_setup_texture_state(struct r300_texture
* tex
)
33 struct r300_texture_state
* state
= &tex
->state
;
34 struct pipe_texture
*pt
= &tex
->tex
;
36 state
->format0
= R300_TX_WIDTH((pt
->width
[0] - 1) & 0x7ff) |
37 R300_TX_HEIGHT((pt
->height
[0] - 1) & 0x7ff) |
38 R300_TX_DEPTH(util_logbase2(pt
->depth
[0]) & 0xf) |
39 R300_TX_NUM_LEVELS(pt
->last_level
) |
43 state
->format1
= r300_translate_texformat(pt
->format
);
44 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
45 state
->format1
|= R300_TX_FORMAT_CUBIC_MAP
;
47 if (pt
->target
== PIPE_TEXTURE_3D
) {
48 state
->format1
|= R300_TX_FORMAT_3D
;
51 state
->format2
= (r300_texture_get_stride(tex
, 0) / pt
->block
.size
) - 1;
53 /* Don't worry about accidentally setting this bit on non-r500;
54 * the kernel should catch it. */
55 if (pt
->width
[0] > 2048) {
56 state
->format2
|= R500_TXWIDTH_BIT11
;
58 if (pt
->height
[0] > 2048) {
59 state
->format2
|= R500_TXHEIGHT_BIT11
;
62 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
63 pt
->width
[0], pt
->height
[0], pt
->last_level
);
67 * Return the stride, in bytes, of the texture images of the given texture
70 unsigned r300_texture_get_stride(struct r300_texture
* tex
, unsigned level
)
72 if (tex
->stride_override
)
73 return tex
->stride_override
;
75 if (level
> tex
->tex
.last_level
) {
76 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__
,
77 level
, tex
->tex
.last_level
);
81 return align(pf_get_stride(&tex
->tex
.block
, tex
->tex
.width
[level
]), 32);
84 static void r300_setup_miptree(struct r300_texture
* tex
)
86 struct pipe_texture
* base
= &tex
->tex
;
90 for (i
= 0; i
<= base
->last_level
; i
++) {
92 base
->width
[i
] = minify(base
->width
[i
-1]);
93 base
->height
[i
] = minify(base
->height
[i
-1]);
94 base
->depth
[i
] = minify(base
->depth
[i
-1]);
97 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
98 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->height
[i
]);
100 stride
= r300_texture_get_stride(tex
, i
);
101 size
= stride
* base
->nblocksy
[i
] * base
->depth
[i
];
103 tex
->offset
[i
] = align(tex
->size
, 32);
104 tex
->size
= tex
->offset
[i
] + size
;
106 debug_printf("r300: Texture miptree: Level %d "
107 "(%dx%dx%d px, pitch %d bytes)\n",
108 i
, base
->width
[i
], base
->height
[i
], base
->depth
[i
],
113 /* Create a new texture. */
114 static struct pipe_texture
*
115 r300_texture_create(struct pipe_screen
* screen
,
116 const struct pipe_texture
* template)
118 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
124 tex
->tex
= *template;
125 pipe_reference_init(&tex
->tex
.reference
, 1);
126 tex
->tex
.screen
= screen
;
128 r300_setup_miptree(tex
);
130 r300_setup_texture_state(tex
);
132 tex
->buffer
= screen
->buffer_create(screen
, 1024,
133 PIPE_BUFFER_USAGE_PIXEL
,
141 return (struct pipe_texture
*)tex
;
144 static void r300_texture_destroy(struct pipe_texture
* texture
)
146 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
148 pipe_buffer_reference(&tex
->buffer
, NULL
);
153 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
154 struct pipe_texture
* texture
,
160 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
161 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
164 /* XXX this is certainly dependent on tex target */
165 offset
= tex
->offset
[level
];
168 pipe_reference_init(&surface
->reference
, 1);
169 pipe_texture_reference(&surface
->texture
, texture
);
170 surface
->format
= texture
->format
;
171 surface
->width
= texture
->width
[level
];
172 surface
->height
= texture
->height
[level
];
173 surface
->offset
= offset
;
174 surface
->usage
= flags
;
180 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
182 pipe_texture_reference(&s
->texture
, NULL
);
186 static struct pipe_texture
*
187 r300_texture_blanket(struct pipe_screen
* screen
,
188 const struct pipe_texture
* base
,
189 const unsigned* stride
,
190 struct pipe_buffer
* buffer
)
192 struct r300_texture
* tex
;
194 if (base
->target
!= PIPE_TEXTURE_2D
||
195 base
->depth
[0] != 1) {
199 tex
= CALLOC_STRUCT(r300_texture
);
205 pipe_reference_init(&tex
->tex
.reference
, 1);
206 tex
->tex
.screen
= screen
;
208 tex
->stride_override
= *stride
;
210 r300_setup_texture_state(tex
);
212 pipe_buffer_reference(&tex
->buffer
, buffer
);
214 return (struct pipe_texture
*)tex
;
217 static struct pipe_video_surface
*
218 r300_video_surface_create(struct pipe_screen
*screen
,
219 enum pipe_video_chroma_format chroma_format
,
220 unsigned width
, unsigned height
)
222 struct r300_video_surface
*r300_vsfc
;
223 struct pipe_texture
template;
226 assert(width
&& height
);
228 r300_vsfc
= CALLOC_STRUCT(r300_video_surface
);
232 pipe_reference_init(&r300_vsfc
->base
.reference
, 1);
233 r300_vsfc
->base
.screen
= screen
;
234 r300_vsfc
->base
.chroma_format
= chroma_format
;
235 r300_vsfc
->base
.width
= width
;
236 r300_vsfc
->base
.height
= height
;
238 memset(&template, 0, sizeof(struct pipe_texture
));
239 template.target
= PIPE_TEXTURE_2D
;
240 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
241 template.last_level
= 0;
242 template.width
[0] = util_next_power_of_two(width
);
243 template.height
[0] = util_next_power_of_two(height
);
244 template.depth
[0] = 1;
245 pf_get_block(template.format
, &template.block
);
246 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
|
247 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
249 r300_vsfc
->tex
= screen
->texture_create(screen
, &template);
256 return &r300_vsfc
->base
;
259 static void r300_video_surface_destroy(struct pipe_video_surface
*vsfc
)
261 struct r300_video_surface
*r300_vsfc
= r300_video_surface(vsfc
);
262 pipe_texture_reference(&r300_vsfc
->tex
, NULL
);
266 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
268 screen
->texture_create
= r300_texture_create
;
269 screen
->texture_destroy
= r300_texture_destroy
;
270 screen
->get_tex_surface
= r300_get_tex_surface
;
271 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
272 screen
->texture_blanket
= r300_texture_blanket
;
274 screen
->video_surface_create
= r300_video_surface_create
;
275 screen
->video_surface_destroy
= r300_video_surface_destroy
;
278 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
279 struct pipe_buffer
** buffer
,
282 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
287 pipe_buffer_reference(buffer
, tex
->buffer
);
290 *stride
= r300_texture_get_stride(tex
, 0);