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