tgsi: add caps for fragment coord conventions (v3)
[mesa.git] / src / gallium / drivers / r300 / r300_screen.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
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:
10 *
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
13 * Software.
14 *
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. */
22
23 #include "pipe/p_inlines.h"
24 #include "util/u_format.h"
25 #include "util/u_memory.h"
26 #include "util/u_simple_screen.h"
27
28 #include "r300_context.h"
29 #include "r300_screen.h"
30 #include "r300_texture.h"
31
32 #include "radeon_winsys.h"
33
34 /* Return the identifier behind whom the brave coders responsible for this
35 * amalgamation of code, sweat, and duct tape, routinely obscure their names.
36 *
37 * ...I should have just put "Corbin Simpson", but I'm not that cool.
38 *
39 * (Or egotistical. Yet.) */
40 static const char* r300_get_vendor(struct pipe_screen* pscreen)
41 {
42 return "X.Org R300 Project";
43 }
44
45 static const char* chip_families[] = {
46 "R300",
47 "R350",
48 "R360",
49 "RV350",
50 "RV370",
51 "RV380",
52 "R420",
53 "R423",
54 "R430",
55 "R480",
56 "R481",
57 "RV410",
58 "RS400",
59 "RC410",
60 "RS480",
61 "RS482",
62 "RS600",
63 "RS690",
64 "RS740",
65 "RV515",
66 "R520",
67 "RV530",
68 "R580",
69 "RV560",
70 "RV570"
71 };
72
73 static const char* r300_get_name(struct pipe_screen* pscreen)
74 {
75 struct r300_screen* r300screen = r300_screen(pscreen);
76
77 return chip_families[r300screen->caps->family];
78 }
79
80 static int r300_get_param(struct pipe_screen* pscreen, int param)
81 {
82 struct r300_screen* r300screen = r300_screen(pscreen);
83
84 switch (param) {
85 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
86 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
87 /* XXX I'm told this goes up to 16 */
88 return 8;
89 case PIPE_CAP_NPOT_TEXTURES:
90 /* XXX enable now to get GL2.1 API,
91 * figure out later how to emulate this */
92 return 1;
93 case PIPE_CAP_TWO_SIDED_STENCIL:
94 if (r300screen->caps->is_r500) {
95 return 1;
96 } else {
97 return 0;
98 }
99 case PIPE_CAP_GLSL:
100 /* I'll be frank. This is a lie.
101 *
102 * We don't truly support GLSL on any of this driver's chipsets.
103 * To be fair, no chipset supports the full GLSL specification
104 * to the best of our knowledge, but some of the less esoteric
105 * features are still missing here.
106 *
107 * Rather than cripple ourselves intentionally, I'm going to set
108 * this flag, and as Gallium's interface continues to change, I
109 * hope that this single monolithic GLSL enable can slowly get
110 * split down into many different pieces and the state tracker
111 * will handle fallbacks transparently, like it should.
112 *
113 * ~ C.
114 */
115 return 1;
116 case PIPE_CAP_DUAL_SOURCE_BLEND:
117 /* XXX need to figure out how to do it */
118 return 0;
119 case PIPE_CAP_ANISOTROPIC_FILTER:
120 return 1;
121 case PIPE_CAP_POINT_SPRITE:
122 return 1;
123 case PIPE_CAP_MAX_RENDER_TARGETS:
124 return 4;
125 case PIPE_CAP_OCCLUSION_QUERY:
126 return 1;
127 case PIPE_CAP_TEXTURE_SHADOW_MAP:
128 return 1;
129 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
130 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
131 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
132 if (r300screen->caps->is_r500) {
133 /* 13 == 4096 */
134 return 13;
135 } else {
136 /* 12 == 2048 */
137 return 12;
138 }
139 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
140 return 1;
141 case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
142 return 1;
143 case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
144 return 0;
145 case PIPE_CAP_TGSI_CONT_SUPPORTED:
146 return 0;
147 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
148 return 1;
149 case PIPE_CAP_SM3:
150 if (r300screen->caps->is_r500) {
151 return 1;
152 } else {
153 return 0;
154 }
155 case PIPE_CAP_INDEP_BLEND_ENABLE:
156 return 1;
157 case PIPE_CAP_INDEP_BLEND_FUNC:
158 return 0;
159 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
160 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
161 return 1;
162 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
163 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
164 return 0;
165 default:
166 debug_printf("r300: Implementation error: Bad param %d\n",
167 param);
168 return 0;
169 }
170 }
171
172 static float r300_get_paramf(struct pipe_screen* pscreen, int param)
173 {
174 struct r300_screen* r300screen = r300_screen(pscreen);
175
176 switch (param) {
177 case PIPE_CAP_MAX_LINE_WIDTH:
178 case PIPE_CAP_MAX_LINE_WIDTH_AA:
179 case PIPE_CAP_MAX_POINT_WIDTH:
180 case PIPE_CAP_MAX_POINT_WIDTH_AA:
181 /* The maximum dimensions of the colorbuffer are our practical
182 * rendering limits. 2048 pixels should be enough for anybody. */
183 if (r300screen->caps->is_r500) {
184 return 4096.0f;
185 } else {
186 return 2048.0f;
187 }
188 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
189 return 16.0f;
190 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
191 return 16.0f;
192 default:
193 debug_printf("r300: Implementation error: Bad paramf %d\n",
194 param);
195 return 0.0f;
196 }
197 }
198
199 static boolean r300_is_format_supported(struct pipe_screen* screen,
200 enum pipe_format format,
201 enum pipe_texture_target target,
202 unsigned usage,
203 unsigned geom_flags)
204 {
205 uint32_t retval = 0;
206 boolean is_r500 = r300_screen(screen)->caps->is_r500;
207
208 if (target >= PIPE_MAX_TEXTURE_TYPES) {
209 debug_printf("r300: Implementation error: Received bogus texture "
210 "target %d in %s\n", target, __FUNCTION__);
211 return FALSE;
212 }
213
214 switch (format) {
215 /* Supported formats. */
216 /* Colorbuffer */
217 case PIPE_FORMAT_A4R4G4B4_UNORM:
218 case PIPE_FORMAT_R5G6B5_UNORM:
219 case PIPE_FORMAT_A1R5G5B5_UNORM:
220 retval = usage &
221 (PIPE_TEXTURE_USAGE_RENDER_TARGET |
222 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
223 PIPE_TEXTURE_USAGE_PRIMARY);
224 break;
225
226 /* Texture */
227 case PIPE_FORMAT_A8R8G8B8_SRGB:
228 case PIPE_FORMAT_R8G8B8A8_SRGB:
229 case PIPE_FORMAT_DXT1_RGB:
230 case PIPE_FORMAT_DXT1_RGBA:
231 case PIPE_FORMAT_DXT3_RGBA:
232 case PIPE_FORMAT_DXT5_RGBA:
233 case PIPE_FORMAT_YCBCR:
234 case PIPE_FORMAT_L8_UNORM:
235 case PIPE_FORMAT_A8_UNORM:
236 case PIPE_FORMAT_A8L8_UNORM:
237 retval = usage & PIPE_TEXTURE_USAGE_SAMPLER;
238 break;
239
240 /* Colorbuffer or texture */
241 case PIPE_FORMAT_A8R8G8B8_UNORM:
242 case PIPE_FORMAT_X8R8G8B8_UNORM:
243 case PIPE_FORMAT_R8G8B8A8_UNORM:
244 case PIPE_FORMAT_R8G8B8X8_UNORM:
245 case PIPE_FORMAT_I8_UNORM:
246 retval = usage &
247 (PIPE_TEXTURE_USAGE_RENDER_TARGET |
248 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
249 PIPE_TEXTURE_USAGE_PRIMARY |
250 PIPE_TEXTURE_USAGE_SAMPLER);
251 break;
252
253 /* Z buffer or texture */
254 case PIPE_FORMAT_Z16_UNORM:
255 retval = usage &
256 (PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
257 PIPE_TEXTURE_USAGE_SAMPLER);
258 break;
259
260 /* 24bit Z buffer can only be used as a texture on R500. */
261 case PIPE_FORMAT_Z24X8_UNORM:
262 /* Z buffer with stencil or texture */
263 case PIPE_FORMAT_Z24S8_UNORM:
264 retval = usage &
265 (PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
266 (is_r500 ? PIPE_TEXTURE_USAGE_SAMPLER : 0));
267 break;
268
269 /* Definitely unsupported formats. */
270 /* Non-usable Z buffer/stencil formats. */
271 case PIPE_FORMAT_Z32_UNORM:
272 case PIPE_FORMAT_S8Z24_UNORM:
273 case PIPE_FORMAT_X8Z24_UNORM:
274 SCREEN_DBG(r300_screen(screen), DBG_TEX,
275 "r300: Note: Got unsupported format: %s in %s\n",
276 pf_name(format), __FUNCTION__);
277 return FALSE;
278
279 /* XXX Add all remaining gallium-supported formats,
280 * see util/u_format.csv. */
281
282 default:
283 /* Unknown format... */
284 debug_printf("r300: Warning: Got unknown format: %s in %s\n",
285 pf_name(format), __FUNCTION__);
286 break;
287 }
288
289 /* If usage was a mask that contained multiple bits, and not all of them
290 * are supported, this will catch that and return FALSE.
291 * e.g. usage = 2 | 4; retval = 4; (retval >= usage) == FALSE
292 *
293 * This also returns FALSE for any unknown formats.
294 */
295 return (retval >= usage);
296 }
297
298 static struct pipe_transfer*
299 r300_get_tex_transfer(struct pipe_screen *screen,
300 struct pipe_texture *texture,
301 unsigned face, unsigned level, unsigned zslice,
302 enum pipe_transfer_usage usage, unsigned x, unsigned y,
303 unsigned w, unsigned h)
304 {
305 struct r300_texture *tex = (struct r300_texture *)texture;
306 struct r300_transfer *trans;
307 struct r300_screen *rscreen = r300_screen(screen);
308 unsigned offset;
309
310 offset = r300_texture_get_offset(tex, level, zslice, face); /* in bytes */
311
312 trans = CALLOC_STRUCT(r300_transfer);
313 if (trans) {
314 pipe_texture_reference(&trans->transfer.texture, texture);
315 trans->transfer.x = x;
316 trans->transfer.y = y;
317 trans->transfer.width = w;
318 trans->transfer.height = h;
319 trans->transfer.stride = r300_texture_get_stride(rscreen, tex, level);
320 trans->transfer.usage = usage;
321 trans->transfer.zslice = zslice;
322 trans->transfer.face = face;
323
324 trans->offset = offset;
325 }
326 return &trans->transfer;
327 }
328
329 static void
330 r300_tex_transfer_destroy(struct pipe_transfer *trans)
331 {
332 pipe_texture_reference(&trans->texture, NULL);
333 FREE(trans);
334 }
335
336 static void* r300_transfer_map(struct pipe_screen* screen,
337 struct pipe_transfer* transfer)
338 {
339 struct r300_texture* tex = (struct r300_texture*)transfer->texture;
340 char* map;
341 enum pipe_format format = tex->tex.format;
342
343 map = pipe_buffer_map(screen, tex->buffer,
344 pipe_transfer_buffer_flags(transfer));
345
346 if (!map) {
347 return NULL;
348 }
349
350 return map + r300_transfer(transfer)->offset +
351 transfer->y / util_format_get_blockheight(format) * transfer->stride +
352 transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
353 }
354
355 static void r300_transfer_unmap(struct pipe_screen* screen,
356 struct pipe_transfer* transfer)
357 {
358 struct r300_texture* tex = (struct r300_texture*)transfer->texture;
359 pipe_buffer_unmap(screen, tex->buffer);
360 }
361
362 static void r300_destroy_screen(struct pipe_screen* pscreen)
363 {
364 struct r300_screen* r300screen = r300_screen(pscreen);
365
366 FREE(r300screen->caps);
367 FREE(r300screen);
368 }
369
370 struct pipe_screen* r300_create_screen(struct radeon_winsys* radeon_winsys)
371 {
372 struct r300_screen* r300screen = CALLOC_STRUCT(r300_screen);
373 struct r300_capabilities* caps = CALLOC_STRUCT(r300_capabilities);
374
375 if (!r300screen || !caps)
376 return NULL;
377
378 caps->pci_id = radeon_winsys->pci_id;
379 caps->num_frag_pipes = radeon_winsys->gb_pipes;
380 caps->num_z_pipes = radeon_winsys->z_pipes;
381
382 r300_init_debug(r300screen);
383 r300_parse_chipset(caps);
384
385 r300screen->caps = caps;
386 r300screen->screen.winsys = (struct pipe_winsys*)radeon_winsys;
387 r300screen->screen.destroy = r300_destroy_screen;
388 r300screen->screen.get_name = r300_get_name;
389 r300screen->screen.get_vendor = r300_get_vendor;
390 r300screen->screen.get_param = r300_get_param;
391 r300screen->screen.get_paramf = r300_get_paramf;
392 r300screen->screen.is_format_supported = r300_is_format_supported;
393 r300screen->screen.get_tex_transfer = r300_get_tex_transfer;
394 r300screen->screen.tex_transfer_destroy = r300_tex_transfer_destroy;
395 r300screen->screen.transfer_map = r300_transfer_map;
396 r300screen->screen.transfer_unmap = r300_transfer_unmap;
397
398 r300_init_screen_texture_functions(&r300screen->screen);
399 u_simple_screen_init(&r300screen->screen);
400
401 return &r300screen->screen;
402 }