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