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 void r300_setup_texture_state(struct r300_texture
* tex
,
31 struct r300_texture_state
* state
= &tex
->state
;
33 state
->format0
= R300_TX_WIDTH((width
- 1) & 0x7ff) |
34 R300_TX_HEIGHT((height
- 1) & 0x7ff) |
35 R300_TX_NUM_LEVELS(levels
) |
39 state
->format1
= r300_translate_texformat(tex
->tex
.format
);
41 state
->format2
= pitch
- 1;
43 /* Assume (somewhat foolishly) that oversized textures will
44 * not be permitted by the state tracker. */
46 state
->format2
|= R500_TXWIDTH_BIT11
;
49 state
->format2
|= R500_TXHEIGHT_BIT11
;
52 debug_printf("r300: Set texture state (%dx%d, pitch %d, %d levels)\n",
53 width
, height
, pitch
, levels
);
56 static void r300_setup_miptree(struct r300_texture
* tex
)
58 struct pipe_texture
* base
= &tex
->tex
;
59 int stride
, size
, offset
;
62 for (i
= 0; i
<= base
->last_level
; i
++) {
64 base
->width
[i
] = minify(base
->width
[i
-1]);
65 base
->height
[i
] = minify(base
->height
[i
-1]);
66 base
->depth
[i
] = minify(base
->depth
[i
-1]);
69 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
70 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->height
[i
]);
72 /* Radeons enjoy things in multiples of 64.
75 * POT, uncompressed, unmippmapped textures can be aligned to 32,
77 stride
= align(pf_get_stride(&base
->block
, base
->width
[i
]), 32);
78 size
= stride
* base
->nblocksy
[i
] * base
->depth
[i
];
80 tex
->offset
[i
] = align(tex
->size
, 32);
81 tex
->size
= tex
->offset
[i
] + size
;
83 debug_printf("r300: Texture miptree: Level %d "
84 "(%dx%dx%d px, pitch %d bytes)\n",
85 i
, base
->width
[i
], base
->height
[i
], base
->depth
[i
],
87 /* Save stride of first level to the texture. */
94 /* Create a new texture. */
95 static struct pipe_texture
*
96 r300_texture_create(struct pipe_screen
* screen
,
97 const struct pipe_texture
* template)
99 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
105 tex
->tex
= *template;
106 pipe_reference_init(&tex
->tex
.reference
, 1);
107 tex
->tex
.screen
= screen
;
109 r300_setup_miptree(tex
);
111 r300_setup_texture_state(tex
, template->width
[0], template->height
[0],
112 template->width
[0], template->last_level
);
114 tex
->buffer
= screen
->buffer_create(screen
, 1024,
115 PIPE_BUFFER_USAGE_PIXEL
,
123 return (struct pipe_texture
*)tex
;
126 static void r300_texture_destroy(struct pipe_texture
* texture
)
128 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
130 pipe_buffer_reference(&tex
->buffer
, NULL
);
135 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
136 struct pipe_texture
* texture
,
142 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
143 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
146 /* XXX this is certainly dependent on tex target */
147 offset
= tex
->offset
[level
];
150 pipe_reference_init(&surface
->reference
, 1);
151 pipe_texture_reference(&surface
->texture
, texture
);
152 surface
->format
= texture
->format
;
153 surface
->width
= texture
->width
[level
];
154 surface
->height
= texture
->height
[level
];
155 surface
->offset
= offset
;
156 surface
->usage
= flags
;
162 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
164 pipe_texture_reference(&s
->texture
, NULL
);
168 static struct pipe_texture
*
169 r300_texture_blanket(struct pipe_screen
* screen
,
170 const struct pipe_texture
* base
,
171 const unsigned* stride
,
172 struct pipe_buffer
* buffer
)
174 struct r300_texture
* tex
;
176 /* XXX we should start doing mips now... */
177 if (base
->target
!= PIPE_TEXTURE_2D
||
178 base
->last_level
!= 0 ||
179 base
->depth
[0] != 1) {
183 tex
= CALLOC_STRUCT(r300_texture
);
189 pipe_reference_init(&tex
->tex
.reference
, 1);
190 tex
->tex
.screen
= screen
;
192 tex
->stride
= *stride
;
195 r300_setup_texture_state(tex
, tex
->tex
.width
[0], tex
->tex
.height
[0],
198 pipe_buffer_reference(&tex
->buffer
, buffer
);
200 return (struct pipe_texture
*)tex
;
203 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
205 screen
->texture_create
= r300_texture_create
;
206 screen
->texture_destroy
= r300_texture_destroy
;
207 screen
->get_tex_surface
= r300_get_tex_surface
;
208 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
209 screen
->texture_blanket
= r300_texture_blanket
;
212 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
213 struct pipe_buffer
** buffer
,
216 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
221 pipe_buffer_reference(buffer
, tex
->buffer
);
224 *stride
= tex
->stride
;