gallium: remove some debug assertions in vertex format validation
[mesa.git] / src / mesa / pipe / softpipe / sp_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
31 */
32
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/p_util.h"
37 #include "pipe/p_winsys.h"
38
39 #include "sp_context.h"
40 #include "sp_state.h"
41 #include "sp_texture.h"
42
43
44 /* Simple, maximally packed layout.
45 */
46
47 static unsigned minify( unsigned d )
48 {
49 return MAX2(1, d>>1);
50 }
51
52
53 static void
54 softpipe_texture_layout(struct softpipe_texture * spt)
55 {
56 struct pipe_texture *pt = &spt->base;
57 unsigned level;
58 unsigned width = pt->width[0];
59 unsigned height = pt->height[0];
60 unsigned depth = pt->depth[0];
61
62 spt->buffer_size = 0;
63
64 for (level = 0; level <= pt->last_level; level++) {
65 pt->width[level] = width;
66 pt->height[level] = height;
67 pt->depth[level] = depth;
68
69 spt->level_offset[level] = spt->buffer_size;
70
71 spt->buffer_size += ((pt->compressed) ? MAX2(1, height/4) : height) *
72 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
73 width * pt->cpp;
74
75 width = minify(width);
76 height = minify(height);
77 depth = minify(depth);
78 }
79 }
80
81
82 struct pipe_texture *
83 softpipe_texture_create(struct pipe_context *pipe,
84 const struct pipe_texture *templat)
85 {
86 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
87 if (!spt)
88 return NULL;
89
90 spt->base = *templat;
91
92 softpipe_texture_layout(spt);
93
94 spt->buffer = pipe->winsys->buffer_create(pipe->winsys, 32,
95 PIPE_BUFFER_USAGE_PIXEL,
96 spt->buffer_size);
97 if (!spt->buffer) {
98 FREE(spt);
99 return NULL;
100 }
101
102 return &spt->base;
103 }
104
105
106 void
107 softpipe_texture_release(struct pipe_context *pipe, struct pipe_texture **pt)
108 {
109 if (!*pt)
110 return;
111
112 /*
113 DBG("%s %p refcount will be %d\n",
114 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
115 */
116 if (--(*pt)->refcount <= 0) {
117 struct softpipe_texture *spt = softpipe_texture(*pt);
118
119 /*
120 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
121 */
122
123 pipe_buffer_reference(pipe->winsys, &spt->buffer, NULL);
124
125 FREE(spt);
126 }
127 *pt = NULL;
128 }
129
130
131 /**
132 * Called via pipe->get_tex_surface()
133 */
134 struct pipe_surface *
135 softpipe_get_tex_surface(struct pipe_context *pipe,
136 struct pipe_texture *pt,
137 unsigned face, unsigned level, unsigned zslice)
138 {
139 struct softpipe_texture *spt = softpipe_texture(pt);
140 struct pipe_surface *ps;
141
142 assert(level <= pt->last_level);
143
144 ps = pipe->winsys->surface_alloc(pipe->winsys);
145 if (ps) {
146 assert(ps->refcount);
147 assert(ps->winsys);
148 pipe_buffer_reference(pipe->winsys, &ps->buffer, spt->buffer);
149 ps->format = pt->format;
150 ps->cpp = pt->cpp;
151 ps->width = pt->width[level];
152 ps->height = pt->height[level];
153 ps->pitch = ps->width;
154 ps->offset = spt->level_offset[level];
155
156 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
157 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
158 (pt->compressed ? ps->height/4 : ps->height) *
159 ps->width * ps->cpp;
160 } else {
161 assert(face == 0);
162 assert(zslice == 0);
163 }
164 }
165 return ps;
166 }