1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
35 #include "fd3_texture.h"
38 static enum a3xx_tex_clamp
39 tex_clamp(unsigned wrap
)
41 /* hardware probably supports more, but we can't coax all the
42 * wrap/clamp modes out of the GLESv2 blob driver.
44 * TODO once we have basics working, go back and just try
45 * different values and see what happens
48 case PIPE_TEX_WRAP_REPEAT
:
49 return A3XX_TEX_REPEAT
;
50 case PIPE_TEX_WRAP_CLAMP
:
51 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
52 return A3XX_TEX_CLAMP_TO_EDGE
;
53 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
54 return A3XX_TEX_CLAMP_TO_BORDER
;
55 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
56 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
57 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
58 return A3XX_TEX_MIRROR_CLAMP
;
59 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
60 return A3XX_TEX_MIRROR_REPEAT
;
62 DBG("invalid wrap: %u", wrap
);
67 static enum a3xx_tex_filter
68 tex_filter(unsigned filter
)
71 case PIPE_TEX_FILTER_NEAREST
:
72 return A3XX_TEX_NEAREST
;
73 case PIPE_TEX_FILTER_LINEAR
:
74 return A3XX_TEX_LINEAR
;
76 DBG("invalid filter: %u", filter
);
82 fd3_sampler_state_create(struct pipe_context
*pctx
,
83 const struct pipe_sampler_state
*cso
)
85 struct fd3_sampler_stateobj
*so
= CALLOC_STRUCT(fd3_sampler_stateobj
);
86 bool miplinear
= false;
91 if (cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
97 COND(!cso
->normalized_coords
, A3XX_TEX_SAMP_0_UNNORM_COORDS
) |
98 COND(miplinear
, A3XX_TEX_SAMP_0_MIPFILTER_LINEAR
) |
99 A3XX_TEX_SAMP_0_XY_MAG(tex_filter(cso
->mag_img_filter
)) |
100 A3XX_TEX_SAMP_0_XY_MIN(tex_filter(cso
->min_img_filter
)) |
101 A3XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso
->wrap_s
)) |
102 A3XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso
->wrap_t
)) |
103 A3XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso
->wrap_r
));
105 if (cso
->compare_mode
)
106 so
->texsamp0
|= A3XX_TEX_SAMP_0_COMPARE_FUNC(cso
->compare_func
); /* maps 1:1 */
108 if (cso
->min_mip_filter
!= PIPE_TEX_MIPFILTER_NONE
) {
110 A3XX_TEX_SAMP_1_MIN_LOD(cso
->min_lod
) |
111 A3XX_TEX_SAMP_1_MAX_LOD(cso
->max_lod
);
113 so
->texsamp1
= 0x00000000;
119 static enum a3xx_tex_type
120 tex_type(unsigned target
)
126 case PIPE_TEXTURE_1D
:
127 case PIPE_TEXTURE_1D_ARRAY
:
129 case PIPE_TEXTURE_RECT
:
130 case PIPE_TEXTURE_2D
:
131 case PIPE_TEXTURE_2D_ARRAY
:
133 case PIPE_TEXTURE_3D
:
135 case PIPE_TEXTURE_CUBE
:
136 case PIPE_TEXTURE_CUBE_ARRAY
:
137 return A3XX_TEX_CUBE
;
141 static struct pipe_sampler_view
*
142 fd3_sampler_view_create(struct pipe_context
*pctx
, struct pipe_resource
*prsc
,
143 const struct pipe_sampler_view
*cso
)
145 struct fd3_pipe_sampler_view
*so
= CALLOC_STRUCT(fd3_pipe_sampler_view
);
146 struct fd_resource
*rsc
= fd_resource(prsc
);
147 unsigned miplevels
= cso
->u
.tex
.last_level
- cso
->u
.tex
.first_level
;
153 pipe_reference(NULL
, &prsc
->reference
);
154 so
->base
.texture
= prsc
;
155 so
->base
.reference
.count
= 1;
156 so
->base
.context
= pctx
;
158 so
->tex_resource
= rsc
;
159 so
->mipaddrs
= 1 + miplevels
;
162 A3XX_TEX_CONST_0_TYPE(tex_type(prsc
->target
)) |
163 A3XX_TEX_CONST_0_FMT(fd3_pipe2tex(cso
->format
)) |
164 A3XX_TEX_CONST_0_MIPLVLS(miplevels
) |
165 fd3_tex_swiz(cso
->format
, cso
->swizzle_r
, cso
->swizzle_g
,
166 cso
->swizzle_b
, cso
->swizzle_a
);
168 if (util_format_is_srgb(cso
->format
))
169 so
->texconst0
|= A3XX_TEX_CONST_0_SRGB
;
172 A3XX_TEX_CONST_1_FETCHSIZE(fd3_pipe2fetchsize(cso
->format
)) |
173 A3XX_TEX_CONST_1_WIDTH(prsc
->width0
) |
174 A3XX_TEX_CONST_1_HEIGHT(prsc
->height0
);
175 /* when emitted, A3XX_TEX_CONST_2_INDX() must be OR'd in: */
177 A3XX_TEX_CONST_2_PITCH(rsc
->slices
[0].pitch
* rsc
->cpp
);
178 so
->texconst3
= 0x00000000; /* ??? */
184 fd3_texture_init(struct pipe_context
*pctx
)
186 pctx
->create_sampler_state
= fd3_sampler_state_create
;
187 pctx
->create_sampler_view
= fd3_sampler_view_create
;