gallium: change set_constant_buffer to be UBO-friendly
[mesa.git] / src / gallium / tests / graw / quad-tex.c
1 /* Display a cleared blue window. This demo has no dependencies on
2 * any utility code, just the graw interface and gallium.
3 */
4
5 #include "graw_util.h"
6
7 static const int WIDTH = 300;
8 static const int HEIGHT = 300;
9
10 static struct graw_info info;
11
12
13 static struct pipe_resource *texture = NULL;
14 static struct pipe_sampler_view *sv = NULL;
15 static void *sampler = NULL;
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
39
40 static void set_vertices( void )
41 {
42 struct pipe_vertex_element ve[2];
43 struct pipe_vertex_buffer vbuf;
44 void *handle;
45
46 memset(ve, 0, sizeof ve);
47
48 ve[0].src_offset = Offset(struct vertex, position);
49 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
50 ve[1].src_offset = Offset(struct vertex, color);
51 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
52
53 handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
54 info.ctx->bind_vertex_elements_state(info.ctx, handle);
55
56
57 vbuf.stride = sizeof( struct vertex );
58 vbuf.buffer_offset = 0;
59 vbuf.buffer = info.screen->user_buffer_create(info.screen,
60 vertices,
61 sizeof(vertices),
62 PIPE_BIND_VERTEX_BUFFER);
63
64 info.ctx->set_vertex_buffers(info.ctx, 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 TEMP[0]\n"
92 "DCL SAMP[0]\n"
93 " 0: TXP TEMP[0], IN[0], SAMP[0], 2D\n"
94 " 1: MOV OUT[0], TEMP[0]\n"
95 " 2: END\n";
96
97 handle = graw_parse_fragment_shader(info.ctx, text);
98 info.ctx->bind_fs_state(info.ctx, handle);
99 }
100
101
102 static void draw( void )
103 {
104 union pipe_color_union clear_color = { {.5,.5,.5,1} };
105
106 info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
107 util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, 4);
108 info.ctx->flush(info.ctx, NULL);
109
110 graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL);
111
112 graw_util_flush_front(&info);
113 }
114
115
116 #define SIZE 16
117
118 static void init_tex( void )
119 {
120 ubyte tex2d[SIZE][SIZE][4];
121 int s, t;
122
123 #if (SIZE != 2)
124 for (s = 0; s < SIZE; s++) {
125 for (t = 0; t < SIZE; t++) {
126 if (0) {
127 int x = (s ^ t) & 1;
128 tex2d[t][s][0] = (x) ? 0 : 63;
129 tex2d[t][s][1] = (x) ? 0 : 128;
130 tex2d[t][s][2] = 0;
131 tex2d[t][s][3] = 0xff;
132 }
133 else {
134 int x = ((s ^ t) >> 2) & 1;
135 tex2d[t][s][0] = s*255/(SIZE-1);
136 tex2d[t][s][1] = t*255/(SIZE-1);
137 tex2d[t][s][2] = (x) ? 0 : 128;
138 tex2d[t][s][3] = 0xff;
139 }
140 }
141 }
142 #else
143 tex2d[0][0][0] = 0;
144 tex2d[0][0][1] = 255;
145 tex2d[0][0][2] = 255;
146 tex2d[0][0][3] = 0;
147
148 tex2d[0][1][0] = 0;
149 tex2d[0][1][1] = 0;
150 tex2d[0][1][2] = 255;
151 tex2d[0][1][3] = 255;
152
153 tex2d[1][0][0] = 255;
154 tex2d[1][0][1] = 255;
155 tex2d[1][0][2] = 0;
156 tex2d[1][0][3] = 255;
157
158 tex2d[1][1][0] = 255;
159 tex2d[1][1][1] = 0;
160 tex2d[1][1][2] = 0;
161 tex2d[1][1][3] = 255;
162 #endif
163
164 texture = graw_util_create_tex2d(&info, SIZE, SIZE,
165 PIPE_FORMAT_B8G8R8A8_UNORM, tex2d);
166
167 sv = graw_util_create_simple_sampler_view(&info, texture);
168 info.ctx->set_fragment_sampler_views(info.ctx, 1, &sv);
169
170 sampler = graw_util_create_simple_sampler(&info,
171 PIPE_TEX_WRAP_REPEAT,
172 PIPE_TEX_FILTER_NEAREST);
173 info.ctx->bind_fragment_sampler_states(info.ctx, 1, &sampler);
174 }
175
176
177 static void init( void )
178 {
179 if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
180 exit(1);
181
182 graw_util_default_state(&info, FALSE);
183
184 {
185 struct pipe_rasterizer_state rasterizer;
186 void *handle;
187 memset(&rasterizer, 0, sizeof rasterizer);
188 rasterizer.cull_face = PIPE_FACE_NONE;
189 rasterizer.gl_rasterization_rules = 1;
190 rasterizer.depth_clip = 1;
191 handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer);
192 info.ctx->bind_rasterizer_state(info.ctx, handle);
193 }
194
195 graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000);
196
197 init_tex();
198
199 set_vertices();
200 set_vertex_shader();
201 set_fragment_shader();
202 }
203
204
205 static void args(int argc, char *argv[])
206 {
207 int i;
208
209 for (i = 1; i < argc;) {
210 if (graw_parse_args(&i, argc, argv)) {
211 continue;
212 }
213 exit(1);
214 }
215 }
216
217 int main( int argc, char *argv[] )
218 {
219 args(argc, argv);
220 init();
221
222 graw_set_display_func( draw );
223 graw_main_loop();
224 return 0;
225 }