Merge commit 'origin/7.8'
[mesa.git] / src / gallium / drivers / nvfx / nv30_fragtex.c
1 #include "util/u_format.h"
2
3 #include "nvfx_context.h"
4 #include "nouveau/nouveau_util.h"
5 #include "nvfx_tex.h"
6 #include "nvfx_resource.h"
7
8 void
9 nv30_sampler_state_init(struct pipe_context *pipe,
10 struct nvfx_sampler_state *ps,
11 const struct pipe_sampler_state *cso)
12 {
13 if (cso->max_anisotropy >= 8) {
14 ps->en |= NV34TCL_TX_ENABLE_ANISO_8X;
15 } else
16 if (cso->max_anisotropy >= 4) {
17 ps->en |= NV34TCL_TX_ENABLE_ANISO_4X;
18 } else
19 if (cso->max_anisotropy >= 2) {
20 ps->en |= NV34TCL_TX_ENABLE_ANISO_2X;
21 }
22
23 {
24 float limit;
25
26 limit = CLAMP(cso->lod_bias, -16.0, 15.0);
27 ps->filt |= (int)(cso->lod_bias * 256.0) & 0x1fff;
28
29 limit = CLAMP(cso->max_lod, 0.0, 15.0);
30 ps->en |= (int)(limit) << 14 /*NV34TCL_TX_ENABLE_MIPMAP_MAX_LOD_SHIFT*/;
31
32 limit = CLAMP(cso->min_lod, 0.0, 15.0);
33 ps->en |= (int)(limit) << 26 /*NV34TCL_TX_ENABLE_MIPMAP_MIN_LOD_SHIFT*/;
34 }
35 }
36
37 #define _(m,tf,ts0x,ts0y,ts0z,ts0w,ts1x,ts1y,ts1z,ts1w) \
38 { \
39 TRUE, \
40 PIPE_FORMAT_##m, \
41 NV34TCL_TX_FORMAT_FORMAT_##tf, \
42 (NV34TCL_TX_SWIZZLE_S0_X_##ts0x | NV34TCL_TX_SWIZZLE_S0_Y_##ts0y | \
43 NV34TCL_TX_SWIZZLE_S0_Z_##ts0z | NV34TCL_TX_SWIZZLE_S0_W_##ts0w | \
44 NV34TCL_TX_SWIZZLE_S1_X_##ts1x | NV34TCL_TX_SWIZZLE_S1_Y_##ts1y | \
45 NV34TCL_TX_SWIZZLE_S1_Z_##ts1z | NV34TCL_TX_SWIZZLE_S1_W_##ts1w) \
46 }
47
48 struct nv30_texture_format {
49 boolean defined;
50 uint pipe;
51 int format;
52 int swizzle;
53 };
54
55 static struct nv30_texture_format
56 nv30_texture_formats[] = {
57 _(B8G8R8X8_UNORM, A8R8G8B8, S1, S1, S1, ONE, X, Y, Z, W),
58 _(B8G8R8A8_UNORM, A8R8G8B8, S1, S1, S1, S1, X, Y, Z, W),
59 _(B5G5R5A1_UNORM, A1R5G5B5, S1, S1, S1, S1, X, Y, Z, W),
60 _(B4G4R4A4_UNORM, A4R4G4B4, S1, S1, S1, S1, X, Y, Z, W),
61 _(B5G6R5_UNORM , R5G6B5 , S1, S1, S1, ONE, X, Y, Z, W),
62 _(L8_UNORM , L8 , S1, S1, S1, ONE, X, X, X, X),
63 _(A8_UNORM , L8 , ZERO, ZERO, ZERO, S1, X, X, X, X),
64 _(I8_UNORM , L8 , S1, S1, S1, S1, X, X, X, X),
65 _(L8A8_UNORM , A8L8 , S1, S1, S1, S1, X, X, X, Y),
66 _(Z16_UNORM , R5G6B5 , S1, S1, S1, ONE, X, X, X, X),
67 _(S8_USCALED_Z24_UNORM , A8R8G8B8, S1, S1, S1, ONE, X, X, X, X),
68 _(DXT1_RGB , DXT1 , S1, S1, S1, ONE, X, Y, Z, W),
69 _(DXT1_RGBA , DXT1 , S1, S1, S1, S1, X, Y, Z, W),
70 _(DXT3_RGBA , DXT3 , S1, S1, S1, S1, X, Y, Z, W),
71 _(DXT5_RGBA , DXT5 , S1, S1, S1, S1, X, Y, Z, W),
72 {},
73 };
74
75 static struct nv30_texture_format *
76 nv30_fragtex_format(uint pipe_format)
77 {
78 struct nv30_texture_format *tf = nv30_texture_formats;
79
80 while (tf->defined) {
81 if (tf->pipe == pipe_format)
82 return tf;
83 tf++;
84 }
85
86 NOUVEAU_ERR("unknown texture format %s\n", util_format_name(pipe_format));
87 return NULL;
88 }
89
90
91 struct nouveau_stateobj *
92 nv30_fragtex_build(struct nvfx_context *nvfx, int unit)
93 {
94 struct nvfx_sampler_state *ps = nvfx->tex_sampler[unit];
95 struct nvfx_miptree *nv30mt = (struct nvfx_miptree *)nvfx->fragment_sampler_views[unit]->texture;
96 struct pipe_resource *pt = &nv30mt->base.base;
97 struct nouveau_bo *bo = nv30mt->base.bo;
98 struct nv30_texture_format *tf;
99 struct nouveau_stateobj *so;
100 uint32_t txf, txs;
101 unsigned tex_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_GART | NOUVEAU_BO_RD;
102
103 tf = nv30_fragtex_format(pt->format);
104 if (!tf)
105 return NULL;
106
107 txf = tf->format;
108 txf |= ((pt->last_level>0) ? NV34TCL_TX_FORMAT_MIPMAP : 0);
109 txf |= log2i(pt->width0) << NV34TCL_TX_FORMAT_BASE_SIZE_U_SHIFT;
110 txf |= log2i(pt->height0) << NV34TCL_TX_FORMAT_BASE_SIZE_V_SHIFT;
111 txf |= log2i(pt->depth0) << NV34TCL_TX_FORMAT_BASE_SIZE_W_SHIFT;
112 txf |= NV34TCL_TX_FORMAT_NO_BORDER | 0x10000;
113
114 switch (pt->target) {
115 case PIPE_TEXTURE_CUBE:
116 txf |= NV34TCL_TX_FORMAT_CUBIC;
117 /* fall-through */
118 case PIPE_TEXTURE_2D:
119 txf |= NV34TCL_TX_FORMAT_DIMS_2D;
120 break;
121 case PIPE_TEXTURE_3D:
122 txf |= NV34TCL_TX_FORMAT_DIMS_3D;
123 break;
124 case PIPE_TEXTURE_1D:
125 txf |= NV34TCL_TX_FORMAT_DIMS_1D;
126 break;
127 default:
128 NOUVEAU_ERR("Unknown target %d\n", pt->target);
129 return NULL;
130 }
131
132 txs = tf->swizzle;
133
134 so = so_new(1, 8, 2);
135 so_method(so, nvfx->screen->eng3d, NV34TCL_TX_OFFSET(unit), 8);
136 so_reloc (so, bo, 0, tex_flags | NOUVEAU_BO_LOW, 0, 0);
137 so_reloc (so, bo, txf, tex_flags | NOUVEAU_BO_OR,
138 NV34TCL_TX_FORMAT_DMA0, NV34TCL_TX_FORMAT_DMA1);
139 so_data (so, ps->wrap);
140 so_data (so, NV34TCL_TX_ENABLE_ENABLE | ps->en);
141 so_data (so, txs);
142 so_data (so, ps->filt | 0x2000 /*voodoo*/);
143 so_data (so, (pt->width0 << NV34TCL_TX_NPOT_SIZE_W_SHIFT) |
144 pt->height0);
145 so_data (so, ps->bcol);
146
147 return so;
148 }