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