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