2 * Copyright 2008 Ben Skeggs
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "pipe/p_inlines.h"
27 #include "nv50_context.h"
29 static struct pipe_texture
*
30 nv50_miptree_create(struct pipe_screen
*pscreen
, const struct pipe_texture
*tmp
)
32 struct pipe_winsys
*ws
= pscreen
->winsys
;
33 struct nv50_miptree
*mt
= CALLOC_STRUCT(nv50_miptree
);
34 struct pipe_texture
*pt
= &mt
->base
;
35 unsigned usage
, width
= tmp
->width
[0], height
= tmp
->height
[0];
36 unsigned depth
= tmp
->depth
[0];
40 mt
->base
.refcount
= 1;
41 mt
->base
.screen
= pscreen
;
43 usage
= PIPE_BUFFER_USAGE_PIXEL
;
45 case PIPE_FORMAT_Z24S8_UNORM
:
46 case PIPE_FORMAT_Z16_UNORM
:
47 usage
|= NOUVEAU_BUFFER_USAGE_ZETA
;
55 mt
->image_nr
= pt
->depth
[0];
57 case PIPE_TEXTURE_CUBE
:
65 for (l
= 0; l
<= pt
->last_level
; l
++) {
66 struct nv50_miptree_level
*lvl
= &mt
->level
[l
];
69 pt
->height
[l
] = height
;
71 pt
->nblocksx
[l
] = pf_get_nblocksx(&pt
->block
, width
);
72 pt
->nblocksy
[l
] = pf_get_nblocksy(&pt
->block
, width
);
74 lvl
->image_offset
= CALLOC(mt
->image_nr
, sizeof(int));
75 lvl
->image
= CALLOC(mt
->image_nr
, sizeof(struct pipe_buffer
*));
78 for (i
= 0; i
< mt
->image_nr
; i
++) {
79 for (l
= 0; l
<= pt
->last_level
; l
++) {
80 struct nv50_miptree_level
*lvl
= &mt
->level
[l
];
83 size
= align(pt
->width
[l
], 8) * pt
->block
.size
;
84 size
= align(size
, 64);
85 size
*= align(pt
->height
[l
], 8) * pt
->block
.size
;
87 lvl
->image
[i
] = ws
->buffer_create(ws
, 256, 0, size
);
88 lvl
->image_offset
[i
] = mt
->total_size
;
90 mt
->total_size
+= size
;
94 mt
->buffer
= ws
->buffer_create(ws
, 256, usage
, mt
->total_size
);
104 nv50_miptree_release(struct pipe_screen
*pscreen
, struct pipe_texture
**ppt
)
106 struct pipe_texture
*pt
= *ppt
;
110 if (--pt
->refcount
<= 0) {
111 struct nv50_miptree
*mt
= nv50_miptree(pt
);
113 pipe_buffer_reference(pscreen
, &mt
->buffer
, NULL
);
119 nv50_miptree_sync(struct pipe_screen
*pscreen
, struct nv50_miptree
*mt
,
120 unsigned level
, unsigned image
)
122 struct nouveau_winsys
*nvws
= nv50_screen(pscreen
)->nvws
;
123 struct nv50_miptree_level
*lvl
= &mt
->level
[level
];
124 struct pipe_surface
*dst
, *src
;
125 unsigned face
= 0, zslice
= 0;
127 if (!lvl
->image_dirty_cpu
& (1 << image
))
130 if (mt
->base
.target
== PIPE_TEXTURE_CUBE
)
133 if (mt
->base
.target
== PIPE_TEXTURE_3D
)
136 /* Mark as clean already - so we don't continually call this function
137 * trying to get a GPU_WRITE pipe_surface!
139 lvl
->image_dirty_cpu
&= ~(1 << image
);
141 dst
= pscreen
->get_tex_surface(pscreen
, &mt
->base
, face
, level
, zslice
,
142 PIPE_BUFFER_USAGE_GPU_WRITE
);
143 /* Pretend we're doing CPU access so we get the backing pipe_surface
144 * and not a view into the larger miptree.
146 src
= pscreen
->get_tex_surface(pscreen
, &mt
->base
, face
, level
, zslice
,
147 PIPE_BUFFER_USAGE_CPU_READ
);
149 nvws
->surface_copy(nvws
, dst
, 0, 0, src
, 0, 0, dst
->width
, dst
->height
);
151 pscreen
->tex_surface_release(pscreen
, &dst
);
152 pscreen
->tex_surface_release(pscreen
, &src
);
155 static struct pipe_surface
*
156 nv50_miptree_surface_new(struct pipe_screen
*pscreen
, struct pipe_texture
*pt
,
157 unsigned face
, unsigned level
, unsigned zslice
,
160 struct nv50_miptree
*mt
= nv50_miptree(pt
);
161 struct nv50_miptree_level
*lvl
= &mt
->level
[level
];
162 struct nv50_surface
*s
;
163 struct pipe_surface
*ps
;
166 if (pt
->target
== PIPE_TEXTURE_CUBE
)
169 if (pt
->target
== PIPE_TEXTURE_3D
)
174 s
= CALLOC_STRUCT(nv50_surface
);
180 ps
->winsys
= pscreen
->winsys
;
181 ps
->format
= pt
->format
;
182 ps
->width
= pt
->width
[level
];
183 ps
->height
= pt
->height
[level
];
184 ps
->block
= pt
->block
;
185 ps
->nblocksx
= pt
->nblocksx
[level
];
186 ps
->nblocksy
= pt
->nblocksy
[level
];
187 ps
->stride
= ps
->width
* ps
->block
.size
;
189 ps
->status
= PIPE_SURFACE_STATUS_DEFINED
;
191 if (flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
192 assert(!(flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
));
193 assert(!(lvl
->image_dirty_cpu
& (1 << img
)));
196 pipe_texture_reference(&ps
->texture
, pt
);
197 pipe_buffer_reference(pscreen
, &ps
->buffer
, lvl
->image
[img
]);
199 if (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
)
200 lvl
->image_dirty_cpu
|= (1 << img
);
202 nv50_miptree_sync(pscreen
, mt
, level
, img
);
204 ps
->offset
= lvl
->image_offset
[img
];
205 pipe_texture_reference(&ps
->texture
, pt
);
206 pipe_buffer_reference(pscreen
, &ps
->buffer
, mt
->buffer
);
208 if (flags
& PIPE_BUFFER_USAGE_GPU_WRITE
)
209 lvl
->image_dirty_gpu
|= (1 << img
);
216 nv50_miptree_surface_del(struct pipe_screen
*pscreen
,
217 struct pipe_surface
**psurface
)
219 struct pipe_surface
*ps
= *psurface
;
220 struct nv50_surface
*s
= nv50_surface(ps
);
224 if (--ps
->refcount
<= 0) {
225 pipe_texture_reference(&ps
->texture
, NULL
);
226 pipe_buffer_reference(pscreen
, &ps
->buffer
, NULL
);
232 nv50_screen_init_miptree_functions(struct pipe_screen
*pscreen
)
234 pscreen
->texture_create
= nv50_miptree_create
;
235 pscreen
->texture_release
= nv50_miptree_release
;
236 pscreen
->get_tex_surface
= nv50_miptree_surface_new
;
237 pscreen
->tex_surface_release
= nv50_miptree_surface_del
;