gallium: Replace gl_rasterization_rules with lower_left_origin and half_pixel_center.
[mesa.git] / src / gallium / tests / graw / tex-srgb.c
1 /* Test sRGB texturing.
2 */
3
4 #include "graw_util.h"
5
6
7 static const int WIDTH = 600;
8 static const int HEIGHT = 300;
9
10 static struct graw_info info;
11
12 static struct pipe_resource *texture;
13 static struct pipe_sampler_view *linear_sv, *srgb_sv;
14
15
16 struct vertex {
17 float position[4];
18 float color[4];
19 };
20
21 static struct vertex vertices1[] =
22 {
23 { { -0.1, -0.9, 0.0, 1.0 },
24 { 1, 1, 0, 1 } },
25
26 { { -0.1, 0.9, 0.0, 1.0 },
27 { 1, 0, 0, 1 } },
28
29 { {-0.9, 0.9, 0.0, 1.0 },
30 { 0, 0, 0, 1 } },
31
32 { {-0.9, -0.9, 0.0, 1.0 },
33 { 0, 1, 0, 1 } },
34 };
35
36
37 static struct vertex vertices2[] =
38 {
39 { { 0.9, -0.9, 0.0, 1.0 },
40 { 1, 1, 0, 1 } },
41
42 { { 0.9, 0.9, 0.0, 1.0 },
43 { 1, 0, 0, 1 } },
44
45 { { 0.1, 0.9, 0.0, 1.0 },
46 { 0, 0, 0, 1 } },
47
48 { { 0.1, -0.9, 0.0, 1.0 },
49 { 0, 1, 0, 1 } },
50 };
51
52
53
54
55 static void
56 set_vertices(struct vertex *verts, unsigned num_verts)
57 {
58 struct pipe_vertex_element ve[2];
59 struct pipe_vertex_buffer vbuf;
60 void *handle;
61
62 memset(ve, 0, sizeof ve);
63
64 ve[0].src_offset = Offset(struct vertex, position);
65 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
66 ve[1].src_offset = Offset(struct vertex, color);
67 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
68
69 handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
70 info.ctx->bind_vertex_elements_state(info.ctx, handle);
71
72 memset(&vbuf, 0, sizeof vbuf);
73
74 vbuf.stride = sizeof(struct vertex);
75 vbuf.buffer_offset = 0;
76 vbuf.buffer = pipe_buffer_create_with_data(info.ctx,
77 PIPE_BIND_VERTEX_BUFFER,
78 PIPE_USAGE_STATIC,
79 num_verts * sizeof(struct vertex),
80 verts);
81
82 info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf);
83 }
84
85 static void set_vertex_shader( void )
86 {
87 void *handle;
88 const char *text =
89 "VERT\n"
90 "DCL IN[0]\n"
91 "DCL IN[1]\n"
92 "DCL OUT[0], POSITION\n"
93 "DCL OUT[1], GENERIC[0]\n"
94 " 0: MOV OUT[1], IN[1]\n"
95 " 1: MOV OUT[0], IN[0]\n"
96 " 2: END\n";
97
98 handle = graw_parse_vertex_shader(info.ctx, text);
99 info.ctx->bind_vs_state(info.ctx, handle);
100 }
101
102 static void set_fragment_shader( void )
103 {
104 void *handle;
105 const char *text =
106 "FRAG\n"
107 "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
108 "DCL OUT[0], COLOR\n"
109 "DCL TEMP[0]\n"
110 "DCL SAMP[0]\n"
111 " 0: TXP TEMP[0], IN[0], SAMP[0], 2D\n"
112 " 1: MOV OUT[0], TEMP[0]\n"
113 " 2: END\n";
114
115 handle = graw_parse_fragment_shader(info.ctx, text);
116 info.ctx->bind_fs_state(info.ctx, handle);
117 }
118
119
120 static void draw( void )
121 {
122 union pipe_color_union clear_color;
123
124 clear_color.f[0] = 0.5;
125 clear_color.f[1] = 0.5;
126 clear_color.f[2] = 0.5;
127 clear_color.f[3] = 1.0;
128
129 info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
130
131 info.ctx->set_fragment_sampler_views(info.ctx, 1, &linear_sv);
132 set_vertices(vertices1, 4);
133 util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4);
134
135 info.ctx->set_fragment_sampler_views(info.ctx, 1, &srgb_sv);
136 set_vertices(vertices2, 4);
137 util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4);
138
139 info.ctx->flush(info.ctx, NULL, 0);
140
141 graw_util_flush_front(&info);
142 }
143
144
145 static void init_tex( void )
146 {
147 #define SIZE 64
148 ubyte tex2d[SIZE][SIZE][4];
149 int s, t;
150
151 for (s = 0; s < SIZE; s++) {
152 for (t = 0; t < SIZE; t++) {
153 tex2d[t][s][0] = 0;
154 tex2d[t][s][1] = s * 255 / SIZE;
155 tex2d[t][s][2] = t * 255 / SIZE;
156 tex2d[t][s][3] = 255;
157 }
158 }
159
160 texture = graw_util_create_tex2d(&info, SIZE, SIZE,
161 PIPE_FORMAT_B8G8R8A8_UNORM, tex2d);
162
163 {
164 void *sampler;
165 sampler = graw_util_create_simple_sampler(&info,
166 PIPE_TEX_WRAP_REPEAT,
167 PIPE_TEX_FILTER_NEAREST);
168 info.ctx->bind_fragment_sampler_states(info.ctx, 1, &sampler);
169 }
170
171 /* linear sampler view */
172 {
173 struct pipe_sampler_view sv_temp;
174 memset(&sv_temp, 0, sizeof sv_temp);
175 sv_temp.format = PIPE_FORMAT_B8G8R8A8_UNORM;
176 sv_temp.texture = texture;
177 sv_temp.swizzle_r = PIPE_SWIZZLE_RED;
178 sv_temp.swizzle_g = PIPE_SWIZZLE_GREEN;
179 sv_temp.swizzle_b = PIPE_SWIZZLE_BLUE;
180 sv_temp.swizzle_a = PIPE_SWIZZLE_ALPHA;
181 linear_sv = info.ctx->create_sampler_view(info.ctx, texture, &sv_temp);
182 if (linear_sv == NULL)
183 exit(0);
184 }
185
186 /* srgb sampler view */
187 {
188 struct pipe_sampler_view sv_temp;
189 memset(&sv_temp, 0, sizeof sv_temp);
190 sv_temp.format = PIPE_FORMAT_B8G8R8A8_SRGB;
191 sv_temp.texture = texture;
192 sv_temp.swizzle_r = PIPE_SWIZZLE_RED;
193 sv_temp.swizzle_g = PIPE_SWIZZLE_GREEN;
194 sv_temp.swizzle_b = PIPE_SWIZZLE_BLUE;
195 sv_temp.swizzle_a = PIPE_SWIZZLE_ALPHA;
196 srgb_sv = info.ctx->create_sampler_view(info.ctx, texture, &sv_temp);
197 if (srgb_sv == NULL)
198 exit(0);
199 }
200 #undef SIZE
201 }
202
203 static void init( void )
204 {
205 if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
206 exit(1);
207
208 graw_util_default_state(&info, FALSE);
209
210 graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 10000);
211
212 init_tex();
213
214 set_vertex_shader();
215 set_fragment_shader();
216 }
217
218
219 int main( int argc, char *argv[] )
220 {
221 init();
222
223 graw_set_display_func( draw );
224 graw_main_loop();
225 return 0;
226 }