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 /* Pretend we're doing CPU access so we get the backing pipe_surface
142 * and not a view into the larger miptree.
144 src
= pscreen
->get_tex_surface(pscreen
, &mt
->base
, face
, level
, zslice
,
145 PIPE_BUFFER_USAGE_CPU_READ
);
147 dst
= pscreen
->get_tex_surface(pscreen
, &mt
->base
, face
, level
, zslice
,
148 PIPE_BUFFER_USAGE_GPU_WRITE
);
150 nvws
->surface_copy(nvws
, dst
, 0, 0, src
, 0, 0, dst
->width
, dst
->height
);
152 pscreen
->tex_surface_release(pscreen
, &dst
);
153 pscreen
->tex_surface_release(pscreen
, &src
);
156 static struct pipe_surface
*
157 nv50_miptree_surface_new(struct pipe_screen
*pscreen
, struct pipe_texture
*pt
,
158 unsigned face
, unsigned level
, unsigned zslice
,
161 struct nv50_miptree
*mt
= nv50_miptree(pt
);
162 struct nv50_miptree_level
*lvl
= &mt
->level
[level
];
163 struct nv50_surface
*s
;
164 struct pipe_surface
*ps
;
167 if (pt
->target
== PIPE_TEXTURE_CUBE
)
170 if (pt
->target
== PIPE_TEXTURE_3D
)
175 s
= CALLOC_STRUCT(nv50_surface
);
181 ps
->winsys
= pscreen
->winsys
;
182 ps
->format
= pt
->format
;
183 ps
->width
= pt
->width
[level
];
184 ps
->height
= pt
->height
[level
];
185 ps
->block
= pt
->block
;
186 ps
->nblocksx
= pt
->nblocksx
[level
];
187 ps
->nblocksy
= pt
->nblocksy
[level
];
188 ps
->stride
= ps
->width
* ps
->block
.size
;
190 ps
->status
= PIPE_SURFACE_STATUS_DEFINED
;
192 if (flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
193 assert(!(flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
));
194 assert(!(lvl
->image_dirty_cpu
& (1 << img
)));
197 pipe_texture_reference(&ps
->texture
, pt
);
198 pipe_buffer_reference(pscreen
, &ps
->buffer
, lvl
->image
[img
]);
200 if (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
)
201 lvl
->image_dirty_cpu
|= (1 << img
);
203 nv50_miptree_sync(pscreen
, mt
, level
, img
);
205 ps
->offset
= lvl
->image_offset
[img
];
206 pipe_texture_reference(&ps
->texture
, pt
);
207 pipe_buffer_reference(pscreen
, &ps
->buffer
, mt
->buffer
);
209 if (flags
& PIPE_BUFFER_USAGE_GPU_WRITE
)
210 lvl
->image_dirty_gpu
|= (1 << img
);
217 nv50_miptree_surface_del(struct pipe_screen
*pscreen
,
218 struct pipe_surface
**psurface
)
220 struct pipe_surface
*ps
= *psurface
;
221 struct nv50_surface
*s
= nv50_surface(ps
);
225 if (--ps
->refcount
<= 0) {
226 pipe_texture_reference(&ps
->texture
, NULL
);
227 pipe_buffer_reference(pscreen
, &ps
->buffer
, NULL
);
233 nv50_screen_init_miptree_functions(struct pipe_screen
*pscreen
)
235 pscreen
->texture_create
= nv50_miptree_create
;
236 pscreen
->texture_release
= nv50_miptree_release
;
237 pscreen
->get_tex_surface
= nv50_miptree_surface_new
;
238 pscreen
->tex_surface_release
= nv50_miptree_surface_del
;