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 /* XXX maths need to go to util */
27 static int minify(int i
)
29 return MAX2(1, i
>> 1);
32 static void r300_setup_texture_state(struct r300_texture
* tex
,
38 struct r300_texture_state
* state
= &tex
->state
;
40 state
->format0
= R300_TX_WIDTH((width
- 1) & 0x7ff) |
41 R300_TX_HEIGHT((height
- 1) & 0x7ff) |
42 R300_TX_NUM_LEVELS(levels
) |
46 state
->format1
= r300_translate_texformat(tex
->tex
.format
);
48 state
->format2
= pitch
- 1;
50 /* Assume (somewhat foolishly) that oversized textures will
51 * not be permitted by the state tracker. */
53 state
->format2
|= R500_TXWIDTH_BIT11
;
56 state
->format2
|= R500_TXHEIGHT_BIT11
;
59 debug_printf("r300: Set texture state (%dx%d, pitch %d, %d levels)\n",
60 width
, height
, pitch
, levels
);
63 static void r300_setup_miptree(struct r300_texture
* tex
)
65 struct pipe_texture
* base
= &tex
->tex
;
66 int stride
, size
, offset
;
69 for (i
= 0; i
<= base
->last_level
; i
++) {
71 base
->width
[i
] = minify(base
->width
[i
-1]);
72 base
->height
[i
] = minify(base
->height
[i
-1]);
73 base
->depth
[i
] = minify(base
->depth
[i
-1]);
76 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
77 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->height
[i
]);
79 /* Radeons enjoy things in multiples of 64.
82 * POT, uncompressed, unmippmapped textures can be aligned to 32,
85 (base
->nblocksx
[i
] * base
->block
.size
) / base
->block
.width
,
87 size
= stride
* base
->nblocksy
[i
] * base
->depth
[i
];
89 tex
->offset
[i
] = align(tex
->size
, 32);
90 tex
->size
+= tex
->offset
[i
] + size
;
92 debug_printf("r300: Texture miptree: Level %d "
93 "(%dx%dx%d px, pitch %d bytes)\n",
94 i
, base
->width
[i
], base
->height
[i
], base
->depth
[i
],
96 /* Save stride of first level to the texture. */
103 /* Create a new texture. */
104 static struct pipe_texture
*
105 r300_texture_create(struct pipe_screen
* screen
,
106 const struct pipe_texture
* template)
108 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
114 tex
->tex
= *template;
115 pipe_reference_init(&tex
->tex
.reference
, 1);
116 tex
->tex
.screen
= screen
;
118 r300_setup_miptree(tex
);
120 r300_setup_texture_state(tex
, template->width
[0], template->height
[0],
121 template->width
[0], template->last_level
);
123 tex
->buffer
= screen
->buffer_create(screen
, 1024,
124 PIPE_BUFFER_USAGE_PIXEL
,
132 return (struct pipe_texture
*)tex
;
135 static void r300_texture_destroy(struct pipe_texture
* texture
)
137 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
139 pipe_buffer_reference(&tex
->buffer
, NULL
);
144 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
145 struct pipe_texture
* texture
,
151 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
152 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
155 /* XXX this is certainly dependent on tex target */
156 offset
= tex
->offset
[level
];
159 pipe_reference_init(&surface
->reference
, 1);
160 pipe_texture_reference(&surface
->texture
, texture
);
161 surface
->format
= texture
->format
;
162 surface
->width
= texture
->width
[level
];
163 surface
->height
= texture
->height
[level
];
164 surface
->offset
= offset
;
165 surface
->usage
= flags
;
171 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
173 pipe_texture_reference(&s
->texture
, NULL
);
177 static struct pipe_texture
*
178 r300_texture_blanket(struct pipe_screen
* screen
,
179 const struct pipe_texture
* base
,
180 const unsigned* stride
,
181 struct pipe_buffer
* buffer
)
183 struct r300_texture
* tex
;
185 /* XXX we should start doing mips now... */
186 if (base
->target
!= PIPE_TEXTURE_2D
||
187 base
->last_level
!= 0 ||
188 base
->depth
[0] != 1) {
192 tex
= CALLOC_STRUCT(r300_texture
);
198 pipe_reference_init(&tex
->tex
.reference
, 1);
199 tex
->tex
.screen
= screen
;
201 tex
->stride
= *stride
;
204 r300_setup_texture_state(tex
, tex
->tex
.width
[0], tex
->tex
.height
[0],
207 pipe_buffer_reference(&tex
->buffer
, buffer
);
209 return (struct pipe_texture
*)tex
;
212 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
214 screen
->texture_create
= r300_texture_create
;
215 screen
->texture_destroy
= r300_texture_destroy
;
216 screen
->get_tex_surface
= r300_get_tex_surface
;
217 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
218 screen
->texture_blanket
= r300_texture_blanket
;
221 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
222 struct pipe_buffer
** buffer
,
225 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
230 pipe_buffer_reference(buffer
, tex
->buffer
);
233 *stride
= tex
->stride
;