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_texture_state(struct r300_texture
* tex
,
35 struct r300_texture_state
* state
= &tex
->state
;
37 state
->format0
= R300_TX_WIDTH((width
- 1) & 0x7ff) |
38 R300_TX_HEIGHT((height
- 1) & 0x7ff) | R300_TX_PITCH_EN
;
41 state
->format1
= R300_TX_FORMAT_A8R8G8B8
;
43 state
->format2
= pitch
- 1;
47 state->pitch |= R300_TXWIDTH_11;
50 state->pitch |= R300_TXHEIGHT_11;
54 static void r300_setup_miptree(struct r300_texture
* tex
)
56 struct pipe_texture
* base
= &tex
->tex
;
57 int stride
, size
, offset
;
60 for (i
= 0; i
<= base
->last_level
; i
++) {
62 base
->width
[i
] = minify(base
->width
[i
-1]);
63 base
->height
[i
] = minify(base
->height
[i
-1]);
64 base
->depth
[i
] = minify(base
->depth
[i
-1]);
67 base
->nblocksx
[i
] = pf_get_nblocksx(&base
->block
, base
->width
[i
]);
68 base
->nblocksy
[i
] = pf_get_nblocksy(&base
->block
, base
->width
[i
]);
70 /* Radeons enjoy things in multiples of 32. */
71 /* XXX this can be 32 when POT */
72 stride
= (base
->nblocksx
[i
] * base
->block
.size
+ 63) & ~63;
73 size
= stride
* base
->nblocksy
[i
] * base
->depth
[i
];
75 tex
->offset
[i
] = (tex
->size
+ 63) & ~63;
76 tex
->size
= tex
->offset
[i
] + size
;
84 /* Create a new texture. */
85 static struct pipe_texture
*
86 r300_texture_create(struct pipe_screen
* screen
,
87 const struct pipe_texture
* template)
89 /* XXX struct r300_screen* r300screen = r300_screen(screen); */
91 struct r300_texture
* tex
= CALLOC_STRUCT(r300_texture
);
98 pipe_reference_init(&tex
->tex
.reference
, 1);
99 tex
->tex
.screen
= screen
;
101 r300_setup_miptree(tex
);
104 r300_setup_texture_state(tex
, tex
->tex
.width
[0], tex
->tex
.height
[0],
107 tex
->buffer
= screen
->buffer_create(screen
, 64,
108 PIPE_BUFFER_USAGE_PIXEL
,
116 return (struct pipe_texture
*)tex
;
119 static void r300_texture_destroy(struct pipe_texture
* texture
)
121 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
123 pipe_buffer_reference(&tex
->buffer
, NULL
);
128 static struct pipe_surface
* r300_get_tex_surface(struct pipe_screen
* screen
,
129 struct pipe_texture
* texture
,
135 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
136 struct pipe_surface
* surface
= CALLOC_STRUCT(pipe_surface
);
139 /* XXX this is certainly dependent on tex target */
140 offset
= tex
->offset
[level
];
143 pipe_reference_init(&surface
->reference
, 1);
144 pipe_texture_reference(&surface
->texture
, texture
);
145 surface
->format
= texture
->format
;
146 surface
->width
= texture
->width
[level
];
147 surface
->height
= texture
->height
[level
];
148 surface
->offset
= offset
;
149 surface
->usage
= flags
;
155 static void r300_tex_surface_destroy(struct pipe_surface
* s
)
157 pipe_texture_reference(&s
->texture
, NULL
);
161 static struct pipe_texture
*
162 r300_texture_blanket(struct pipe_screen
* screen
,
163 const struct pipe_texture
* base
,
164 const unsigned* stride
,
165 struct pipe_buffer
* buffer
)
167 struct r300_texture
* tex
;
169 if (base
->target
!= PIPE_TEXTURE_2D
||
170 base
->last_level
!= 0 ||
171 base
->depth
[0] != 1) {
175 tex
= CALLOC_STRUCT(r300_texture
);
181 pipe_reference_init(&tex
->tex
.reference
, 1);
182 tex
->tex
.screen
= screen
;
184 tex
->stride
= *stride
;
186 r300_setup_texture_state(tex
, tex
->tex
.width
[0], tex
->tex
.height
[0],
189 pipe_buffer_reference(&tex
->buffer
, buffer
);
191 return (struct pipe_texture
*)tex
;
194 void r300_init_screen_texture_functions(struct pipe_screen
* screen
)
196 screen
->texture_create
= r300_texture_create
;
197 screen
->texture_destroy
= r300_texture_destroy
;
198 screen
->get_tex_surface
= r300_get_tex_surface
;
199 screen
->tex_surface_destroy
= r300_tex_surface_destroy
;
200 screen
->texture_blanket
= r300_texture_blanket
;
203 boolean
r300_get_texture_buffer(struct pipe_texture
* texture
,
204 struct pipe_buffer
** buffer
,
207 struct r300_texture
* tex
= (struct r300_texture
*)texture
;
212 pipe_buffer_reference(buffer
, tex
->buffer
);
215 *stride
= tex
->stride
;