1/*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27#include "nouveau_driver.h"
28#include "nouveau_context.h"
29#include "nouveau_texture.h"
30#include "nouveau_util.h"
31#include "nouveau_gldefs.h"
32#include "nv_object.xml.h"
33#include "nv04_3d.xml.h"
34#include "nv04_driver.h"
35#include "main/samplerobj.h"
36
37static uint32_t
38get_tex_format(struct gl_texture_image *ti)
39{
40	switch (ti->TexFormat) {
41	case MESA_FORMAT_A_UNORM8:
42	case MESA_FORMAT_L_UNORM8:
43	case MESA_FORMAT_I_UNORM8:
44		return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_Y8;
45	case MESA_FORMAT_B5G5R5A1_UNORM:
46		return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A1R5G5B5;
47	case MESA_FORMAT_B4G4R4A4_UNORM:
48		return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A4R4G4B4;
49	case MESA_FORMAT_B5G6R5_UNORM:
50		return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_R5G6B5;
51	case MESA_FORMAT_B8G8R8A8_UNORM:
52		return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A8R8G8B8;
53	case MESA_FORMAT_B8G8R8X8_UNORM:
54		return NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_X8R8G8B8;
55	default:
56		assert(0);
57	}
58}
59
60void
61nv04_emit_tex_obj(struct gl_context *ctx, int emit)
62{
63	struct nv04_context *nv04 = to_nv04_context(ctx);
64	const int i = emit - NOUVEAU_STATE_TEX_OBJ0;
65	struct nouveau_surface *s;
66	uint32_t format = 0xa0, filter = 0x1010;
67
68	if (ctx->Texture.Unit[i]._Current) {
69		struct gl_texture_object *t = ctx->Texture.Unit[i]._Current;
70		struct gl_texture_image *ti = t->Image[0][t->BaseLevel];
71		const struct gl_sampler_object *sa = _mesa_get_samplerobj(ctx, i);
72		int lod_max = 1, lod_bias = 0;
73
74		if (!nouveau_texture_validate(ctx, t))
75			return;
76
77		s = &to_nouveau_texture(t)->surfaces[t->BaseLevel];
78
79		if (sa->MinFilter != GL_NEAREST &&
80		    sa->MinFilter != GL_LINEAR) {
81			lod_max = CLAMP(MIN2(sa->MaxLod, t->_MaxLambda),
82					0, 15) + 1;
83
84			lod_bias = CLAMP(ctx->Texture.Unit[i].LodBias +
85					 sa->LodBias, -16, 15) * 8;
86		}
87
88		format |= nvgl_wrap_mode(sa->WrapT) << 28 |
89			nvgl_wrap_mode(sa->WrapS) << 24 |
90			ti->HeightLog2 << 20 |
91			ti->WidthLog2 << 16 |
92			lod_max << 12 |
93			get_tex_format(ti);
94
95		filter |= log2i(sa->MaxAnisotropy) << 31 |
96			nvgl_filter_mode(sa->MagFilter) << 28 |
97			log2i(sa->MaxAnisotropy) << 27 |
98			nvgl_filter_mode(sa->MinFilter) << 24 |
99			(lod_bias & 0xff) << 16;
100
101	} else {
102		s = &to_nv04_context(ctx)->dummy_texture;
103
104		format |= NV04_TEXTURED_TRIANGLE_FORMAT_ADDRESSU_REPEAT |
105			NV04_TEXTURED_TRIANGLE_FORMAT_ADDRESSV_REPEAT |
106			1 << 12 |
107			NV04_TEXTURED_TRIANGLE_FORMAT_COLOR_A8R8G8B8;
108
109		filter |= NV04_TEXTURED_TRIANGLE_FILTER_MINIFY_NEAREST |
110			NV04_TEXTURED_TRIANGLE_FILTER_MAGNIFY_NEAREST;
111	}
112
113	nv04->texture[i] = s;
114	nv04->format[i] = format;
115	nv04->filter[i] = filter;
116}
117