Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4358 | Serge | 1 | /* Test texture swizzles */ |
2 | |||
3 | #include |
||
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 | |||
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 = pipe_buffer_create_with_data(info.ctx, |
||
59 | PIPE_BIND_VERTEX_BUFFER, |
||
60 | PIPE_USAGE_STATIC, |
||
61 | sizeof(vertices), |
||
62 | vertices); |
||
63 | |||
64 | info.ctx->set_vertex_buffers(info.ctx, 0, 1, &vbuf); |
||
65 | } |
||
66 | |||
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 | |||
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 | " 0: TXP OUT[0], IN[0], SAMP[0], 2D\n" |
||
93 | " 2: END\n"; |
||
94 | |||
95 | handle = graw_parse_fragment_shader(info.ctx, text); |
||
96 | info.ctx->bind_fs_state(info.ctx, handle); |
||
97 | } |
||
98 | |||
99 | |||
100 | static void draw(void) |
||
101 | { |
||
102 | union pipe_color_union clear_color; |
||
103 | |||
104 | clear_color.f[0] = 0.5; |
||
105 | clear_color.f[1] = 0.5; |
||
106 | clear_color.f[2] = 0.5; |
||
107 | clear_color.f[3] = 1.0; |
||
108 | |||
109 | info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0); |
||
110 | util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4); |
||
111 | info.ctx->flush(info.ctx, NULL, 0); |
||
112 | |||
113 | graw_util_flush_front(&info); |
||
114 | } |
||
115 | |||
116 | |||
117 | |||
118 | static void |
||
119 | init_tex(const unsigned swizzle[4]) |
||
120 | { |
||
121 | #define SIZE 256 |
||
122 | struct pipe_sampler_view sv_template; |
||
123 | ubyte tex2d[SIZE][SIZE][4]; |
||
124 | int s, t; |
||
125 | |||
126 | for (s = 0; s < SIZE; s++) { |
||
127 | for (t = 0; t < SIZE; t++) { |
||
128 | tex2d[t][s][0] = 0; /*B*/ |
||
129 | tex2d[t][s][1] = t; /*G*/ |
||
130 | tex2d[t][s][2] = s; /*R*/ |
||
131 | tex2d[t][s][3] = 1; /*A*/ |
||
132 | } |
||
133 | } |
||
134 | |||
135 | texture = graw_util_create_tex2d(&info, SIZE, SIZE, |
||
136 | PIPE_FORMAT_B8G8R8A8_UNORM, tex2d); |
||
137 | |||
138 | memset(&sv_template, 0, sizeof sv_template); |
||
139 | sv_template.format = texture->format; |
||
140 | sv_template.texture = texture; |
||
141 | sv_template.swizzle_r = swizzle[0]; |
||
142 | sv_template.swizzle_g = swizzle[1]; |
||
143 | sv_template.swizzle_b = swizzle[2]; |
||
144 | sv_template.swizzle_a = swizzle[3]; |
||
145 | sv = info.ctx->create_sampler_view(info.ctx, texture, &sv_template); |
||
146 | if (sv == NULL) |
||
147 | exit(5); |
||
148 | |||
149 | info.ctx->set_fragment_sampler_views(info.ctx, 1, &sv); |
||
150 | |||
151 | sampler = graw_util_create_simple_sampler(&info, |
||
152 | PIPE_TEX_WRAP_REPEAT, |
||
153 | PIPE_TEX_FILTER_NEAREST); |
||
154 | |||
155 | info.ctx->bind_fragment_sampler_states(info.ctx, 1, &sampler); |
||
156 | #undef SIZE |
||
157 | } |
||
158 | |||
159 | |||
160 | static void |
||
161 | init(const unsigned swizzle[4]) |
||
162 | { |
||
163 | if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE)) |
||
164 | exit(1); |
||
165 | |||
166 | graw_util_default_state(&info, FALSE); |
||
167 | |||
168 | graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 10000); |
||
169 | |||
170 | init_tex(swizzle); |
||
171 | |||
172 | set_vertices(); |
||
173 | set_vertex_shader(); |
||
174 | set_fragment_shader(); |
||
175 | } |
||
176 | |||
177 | |||
178 | static unsigned |
||
179 | char_to_swizzle(char c) |
||
180 | { |
||
181 | switch (c) { |
||
182 | case 'r': |
||
183 | return PIPE_SWIZZLE_RED; |
||
184 | case 'g': |
||
185 | return PIPE_SWIZZLE_GREEN; |
||
186 | case 'b': |
||
187 | return PIPE_SWIZZLE_BLUE; |
||
188 | case 'a': |
||
189 | return PIPE_SWIZZLE_ALPHA; |
||
190 | case '0': |
||
191 | return PIPE_SWIZZLE_ZERO; |
||
192 | case '1': |
||
193 | return PIPE_SWIZZLE_ONE; |
||
194 | default: |
||
195 | return PIPE_SWIZZLE_RED; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | |||
200 | int main(int argc, char *argv[]) |
||
201 | { |
||
202 | const char swizzle_names[] = "rgba01"; |
||
203 | uint swizzle[4]; |
||
204 | int i; |
||
205 | |||
206 | swizzle[0] = PIPE_SWIZZLE_RED; |
||
207 | swizzle[1] = PIPE_SWIZZLE_GREEN; |
||
208 | swizzle[2] = PIPE_SWIZZLE_BLUE; |
||
209 | swizzle[3] = PIPE_SWIZZLE_ALPHA; |
||
210 | |||
211 | for (i = 1; i < argc; i++) { |
||
212 | swizzle[i-1] = char_to_swizzle(argv[i][0]); |
||
213 | } |
||
214 | |||
215 | printf("Example:\n"); |
||
216 | printf(" tex-swizzle r 0 g 1\n"); |
||
217 | printf("Current swizzle = "); |
||
218 | for (i = 0; i < 4; i++) { |
||
219 | printf("%c", swizzle_names[swizzle[i]]); |
||
220 | } |
||
221 | printf("\n"); |
||
222 | |||
223 | init(swizzle); |
||
224 | |||
225 | graw_set_display_func(draw); |
||
226 | graw_main_loop(); |
||
227 | return 0; |
||
228 | }>>>> |