1 /* Test texture swizzles */
8 static struct graw_info info
;
10 static struct pipe_resource
*texture
= NULL
;
11 static struct pipe_sampler_view
*sv
= NULL
;
12 static void *sampler
= NULL
;
14 static const int WIDTH
= 300;
15 static const int HEIGHT
= 300;
22 static struct vertex vertices
[] =
24 { { 0.9, -0.9, 0.0, 1.0 },
27 { { 0.9, 0.9, 0.0, 1.0 },
30 { {-0.9, 0.9, 0.0, 1.0 },
33 { {-0.9, -0.9, 0.0, 1.0 },
38 static void set_vertices(void)
40 struct pipe_vertex_element ve
[2];
41 struct pipe_vertex_buffer vbuf
;
44 memset(ve
, 0, sizeof ve
);
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
;
51 handle
= info
.ctx
->create_vertex_elements_state(info
.ctx
, 2, ve
);
52 info
.ctx
->bind_vertex_elements_state(info
.ctx
, handle
);
55 vbuf
.stride
= sizeof(struct vertex
);
56 vbuf
.buffer_offset
= 0;
57 vbuf
.buffer
= info
.screen
->user_buffer_create(info
.screen
,
60 PIPE_BIND_VERTEX_BUFFER
);
62 info
.ctx
->set_vertex_buffers(info
.ctx
, 1, &vbuf
);
65 static void set_vertex_shader(void)
72 "DCL OUT[0], POSITION\n"
73 "DCL OUT[1], GENERIC[0]\n"
74 " 0: MOV OUT[1], IN[1]\n"
75 " 1: MOV OUT[0], IN[0]\n"
78 handle
= graw_parse_vertex_shader(info
.ctx
, text
);
79 info
.ctx
->bind_vs_state(info
.ctx
, handle
);
82 static void set_fragment_shader(void)
87 "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
90 " 0: TXP OUT[0], IN[0], SAMP[0], 2D\n"
93 handle
= graw_parse_fragment_shader(info
.ctx
, text
);
94 info
.ctx
->bind_fs_state(info
.ctx
, handle
);
98 static void draw(void)
100 union pipe_color_union clear_color
;
102 clear_color
.f
[0] = 0.5;
103 clear_color
.f
[1] = 0.5;
104 clear_color
.f
[2] = 0.5;
105 clear_color
.f
[3] = 1.0;
107 info
.ctx
->clear(info
.ctx
, PIPE_CLEAR_COLOR
, &clear_color
, 0, 0);
108 util_draw_arrays(info
.ctx
, PIPE_PRIM_QUADS
, 0, 4);
109 info
.ctx
->flush(info
.ctx
, NULL
);
111 graw_util_flush_front(&info
);
117 init_tex(const unsigned swizzle
[4])
120 struct pipe_sampler_view sv_template
;
121 ubyte tex2d
[SIZE
][SIZE
][4];
124 for (s
= 0; s
< SIZE
; s
++) {
125 for (t
= 0; t
< SIZE
; t
++) {
126 tex2d
[t
][s
][0] = 0; /*B*/
127 tex2d
[t
][s
][1] = t
; /*G*/
128 tex2d
[t
][s
][2] = s
; /*R*/
129 tex2d
[t
][s
][3] = 1; /*A*/
133 texture
= graw_util_create_tex2d(&info
, SIZE
, SIZE
,
134 PIPE_FORMAT_B8G8R8A8_UNORM
, tex2d
);
136 memset(&sv_template
, 0, sizeof sv_template
);
137 sv_template
.format
= texture
->format
;
138 sv_template
.texture
= texture
;
139 sv_template
.swizzle_r
= swizzle
[0];
140 sv_template
.swizzle_g
= swizzle
[1];
141 sv_template
.swizzle_b
= swizzle
[2];
142 sv_template
.swizzle_a
= swizzle
[3];
143 sv
= info
.ctx
->create_sampler_view(info
.ctx
, texture
, &sv_template
);
147 info
.ctx
->set_fragment_sampler_views(info
.ctx
, 1, &sv
);
149 sampler
= graw_util_create_simple_sampler(&info
,
150 PIPE_TEX_WRAP_REPEAT
,
151 PIPE_TEX_FILTER_NEAREST
);
153 info
.ctx
->bind_fragment_sampler_states(info
.ctx
, 1, &sampler
);
159 init(const unsigned swizzle
[4])
161 if (!graw_util_create_window(&info
, WIDTH
, HEIGHT
, 1, FALSE
))
164 graw_util_default_state(&info
, FALSE
);
166 graw_util_viewport(&info
, 0, 0, WIDTH
, HEIGHT
, 30, 10000);
172 set_fragment_shader();
177 char_to_swizzle(char c
)
181 return PIPE_SWIZZLE_RED
;
183 return PIPE_SWIZZLE_GREEN
;
185 return PIPE_SWIZZLE_BLUE
;
187 return PIPE_SWIZZLE_ALPHA
;
189 return PIPE_SWIZZLE_ZERO
;
191 return PIPE_SWIZZLE_ONE
;
193 return PIPE_SWIZZLE_RED
;
198 int main(int argc
, char *argv
[])
200 const char swizzle_names
[] = "rgba01";
204 swizzle
[0] = PIPE_SWIZZLE_RED
;
205 swizzle
[1] = PIPE_SWIZZLE_GREEN
;
206 swizzle
[2] = PIPE_SWIZZLE_BLUE
;
207 swizzle
[3] = PIPE_SWIZZLE_ALPHA
;
209 for (i
= 1; i
< argc
; i
++) {
210 swizzle
[i
-1] = char_to_swizzle(argv
[i
][0]);
213 printf("Example:\n");
214 printf(" tex-swizzle r 0 g 1\n");
215 printf("Current swizzle = ");
216 for (i
= 0; i
< 4; i
++) {
217 printf("%c", swizzle_names
[swizzle
[i
]]);
223 graw_set_display_func(draw
);