225cbfc8e884799a7594fbe566296854a04ef14b
[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 "state_tracker/graw.h"
6 #include "pipe/p_screen.h"
7 #include "pipe/p_context.h"
8 #include "pipe/p_shader_tokens.h"
9 #include "pipe/p_state.h"
10 #include "pipe/p_defines.h"
11 #include <unistd.h> /* for sleep() */
12
13 #include "util/u_debug.h" /* debug_dump_surface_bmp() */
14 #include "util/u_inlines.h"
15 #include "util/u_memory.h" /* Offset() */
16 #include "util/u_box.h"
17
18 enum pipe_format formats[] = {
19 PIPE_FORMAT_R8G8B8A8_UNORM,
20 PIPE_FORMAT_B8G8R8A8_UNORM,
21 PIPE_FORMAT_NONE
22 };
23
24 static const int WIDTH = 300;
25 static const int HEIGHT = 300;
26
27 static struct pipe_screen *screen = NULL;
28 static struct pipe_context *ctx = NULL;
29 static struct pipe_resource *rttex = NULL;
30 static struct pipe_resource *samptex = NULL;
31 static struct pipe_surface *surf = NULL;
32 static struct pipe_sampler_view *sv = NULL;
33 static void *sampler = NULL;
34 static void *window = NULL;
35
36 struct vertex {
37 float position[4];
38 float color[4];
39 };
40
41 static struct vertex vertices[] =
42 {
43 { { 0.9, -0.9, 0.0, 1.0 },
44 { 1, 0, 0, 1 } },
45
46 { { 0.9, 0.9, 0.0, 1.0 },
47 { 1, 1, 0, 1 } },
48
49 { {-0.9, 0.9, 0.0, 1.0 },
50 { 0, 1, 0, 1 } },
51
52 { {-0.9, -0.9, 0.0, 1.0 },
53 { 0, 0, 0, 1 } },
54 };
55
56
57
58
59 static void set_viewport( float x, float y,
60 float width, float height,
61 float near, float far)
62 {
63 float z = far;
64 float half_width = (float)width / 2.0f;
65 float half_height = (float)height / 2.0f;
66 float half_depth = ((float)far - (float)near) / 2.0f;
67 struct pipe_viewport_state vp;
68
69 vp.scale[0] = half_width;
70 vp.scale[1] = half_height;
71 vp.scale[2] = half_depth;
72 vp.scale[3] = 1.0f;
73
74 vp.translate[0] = half_width + x;
75 vp.translate[1] = half_height + y;
76 vp.translate[2] = half_depth + z;
77 vp.translate[3] = 0.0f;
78
79 ctx->set_viewport_state( ctx, &vp );
80 }
81
82 static void set_vertices( void )
83 {
84 struct pipe_vertex_element ve[2];
85 struct pipe_vertex_buffer vbuf;
86 void *handle;
87
88 memset(ve, 0, sizeof ve);
89
90 ve[0].src_offset = Offset(struct vertex, position);
91 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
92 ve[1].src_offset = Offset(struct vertex, color);
93 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
94
95 handle = ctx->create_vertex_elements_state(ctx, 2, ve);
96 ctx->bind_vertex_elements_state(ctx, handle);
97
98
99 vbuf.stride = sizeof( struct vertex );
100 vbuf.max_index = sizeof(vertices) / vbuf.stride;
101 vbuf.buffer_offset = 0;
102 vbuf.buffer = screen->user_buffer_create(screen,
103 vertices,
104 sizeof(vertices),
105 PIPE_BIND_VERTEX_BUFFER);
106
107 ctx->set_vertex_buffers(ctx, 1, &vbuf);
108 }
109
110 static void set_vertex_shader( void )
111 {
112 void *handle;
113 const char *text =
114 "VERT\n"
115 "DCL IN[0]\n"
116 "DCL IN[1]\n"
117 "DCL OUT[0], POSITION\n"
118 "DCL OUT[1], GENERIC[0]\n"
119 " 0: MOV OUT[1], IN[1]\n"
120 " 1: MOV OUT[0], IN[0]\n"
121 " 2: END\n";
122
123 handle = graw_parse_vertex_shader(ctx, text);
124 ctx->bind_vs_state(ctx, handle);
125 }
126
127 static void set_fragment_shader( void )
128 {
129 void *handle;
130 const char *text =
131 "FRAG\n"
132 "DCL IN[0], GENERIC[0], PERSPECTIVE\n"
133 "DCL OUT[0], COLOR\n"
134 "DCL TEMP[0]\n"
135 "DCL SAMP[0]\n"
136 " 0: TXP TEMP[0], IN[0], SAMP[0], 2D\n"
137 " 1: MOV OUT[0], TEMP[0]\n"
138 " 2: END\n";
139
140 handle = graw_parse_fragment_shader(ctx, text);
141 ctx->bind_fs_state(ctx, handle);
142 }
143
144
145 static void draw( void )
146 {
147 float clear_color[4] = {.5,.5,.5,1};
148
149 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
150 ctx->draw_arrays(ctx, PIPE_PRIM_QUADS, 0, 4);
151 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
152
153 #if 0
154 /* At the moment, libgraw leaks out/makes available some of the
155 * symbols from gallium/auxiliary, including these debug helpers.
156 * Will eventually want to bless some of these paths, and lock the
157 * others down so they aren't accessible from test programs.
158 *
159 * This currently just happens to work on debug builds - a release
160 * build will probably fail to link here:
161 */
162 debug_dump_surface_bmp(ctx, "result.bmp", surf);
163 #endif
164
165 screen->flush_frontbuffer(screen, surf, window);
166 }
167
168 #define SIZE 16
169
170 static void init_tex( void )
171 {
172 struct pipe_sampler_view sv_template;
173 struct pipe_sampler_state sampler_desc;
174 struct pipe_resource templat;
175 struct pipe_box box;
176 ubyte tex2d[SIZE][SIZE][4];
177 int s, t;
178
179 #if (SIZE != 2)
180 for (s = 0; s < SIZE; s++) {
181 for (t = 0; t < SIZE; t++) {
182 if (0) {
183 int x = (s ^ t) & 1;
184 tex2d[t][s][0] = (x) ? 0 : 63;
185 tex2d[t][s][1] = (x) ? 0 : 128;
186 tex2d[t][s][2] = 0;
187 tex2d[t][s][3] = 0xff;
188 }
189 else {
190 int x = ((s ^ t) >> 2) & 1;
191 tex2d[t][s][0] = s*255/(SIZE-1);
192 tex2d[t][s][1] = t*255/(SIZE-1);
193 tex2d[t][s][2] = (x) ? 0 : 128;
194 tex2d[t][s][3] = 0xff;
195 }
196 }
197 }
198 #else
199 tex2d[0][0][0] = 0;
200 tex2d[0][0][1] = 255;
201 tex2d[0][0][2] = 255;
202 tex2d[0][0][3] = 0;
203
204 tex2d[0][1][0] = 0;
205 tex2d[0][1][1] = 0;
206 tex2d[0][1][2] = 255;
207 tex2d[0][1][3] = 255;
208
209 tex2d[1][0][0] = 255;
210 tex2d[1][0][1] = 255;
211 tex2d[1][0][2] = 0;
212 tex2d[1][0][3] = 255;
213
214 tex2d[1][1][0] = 255;
215 tex2d[1][1][1] = 0;
216 tex2d[1][1][2] = 0;
217 tex2d[1][1][3] = 255;
218 #endif
219
220 templat.target = PIPE_TEXTURE_2D;
221 templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;
222 templat.width0 = SIZE;
223 templat.height0 = SIZE;
224 templat.depth0 = 1;
225 templat.last_level = 0;
226 templat.nr_samples = 1;
227 templat.bind = PIPE_BIND_SAMPLER_VIEW;
228
229
230 samptex = screen->resource_create(screen,
231 &templat);
232 if (samptex == NULL)
233 exit(4);
234
235 u_box_2d(0,0,SIZE,SIZE, &box);
236
237 ctx->transfer_inline_write(ctx,
238 samptex,
239 u_subresource(0,0),
240 PIPE_TRANSFER_WRITE,
241 &box,
242 tex2d,
243 sizeof tex2d[0],
244 sizeof tex2d);
245
246 /* Possibly read back & compare against original data:
247 */
248 if (0)
249 {
250 struct pipe_transfer *t;
251 uint32_t *ptr;
252 t = pipe_get_transfer(ctx, samptex,
253 0, 0, 0, /* face, level, zslice */
254 PIPE_TRANSFER_READ,
255 0, 0, SIZE, SIZE); /* x, y, width, height */
256
257 ptr = ctx->transfer_map(ctx, t);
258
259 if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {
260 assert(0);
261 exit(9);
262 }
263
264 ctx->transfer_unmap(ctx, t);
265
266 ctx->transfer_destroy(ctx, t);
267 }
268
269 memset(&sv_template, 0, sizeof sv_template);
270 sv_template.format = samptex->format;
271 sv_template.texture = samptex;
272 sv_template.first_level = 0;
273 sv_template.last_level = 0;
274 sv_template.swizzle_r = 0;
275 sv_template.swizzle_g = 1;
276 sv_template.swizzle_b = 2;
277 sv_template.swizzle_a = 3;
278 sv = ctx->create_sampler_view(ctx, samptex, &sv_template);
279 if (sv == NULL)
280 exit(5);
281
282 ctx->set_fragment_sampler_views(ctx, 1, &sv);
283
284
285 memset(&sampler_desc, 0, sizeof sampler_desc);
286 sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;
287 sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;
288 sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;
289 sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;
290 sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
291 sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
292 sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;
293 sampler_desc.compare_func = 0;
294 sampler_desc.normalized_coords = 1;
295 sampler_desc.max_anisotropy = 0;
296
297 sampler = ctx->create_sampler_state(ctx, &sampler_desc);
298 if (sampler == NULL)
299 exit(6);
300
301 ctx->bind_fragment_sampler_states(ctx, 1, &sampler);
302
303 }
304
305 static void init( void )
306 {
307 struct pipe_framebuffer_state fb;
308 struct pipe_resource templat;
309 int i;
310
311 /* It's hard to say whether window or screen should be created
312 * first. Different environments would prefer one or the other.
313 *
314 * Also, no easy way of querying supported formats if the screen
315 * cannot be created first.
316 */
317 for (i = 0;
318 window == NULL && formats[i] != PIPE_FORMAT_NONE;
319 i++) {
320
321 screen = graw_create_window_and_screen(0,0,300,300,
322 formats[i],
323 &window);
324 }
325
326 ctx = screen->context_create(screen, NULL);
327 if (ctx == NULL)
328 exit(3);
329
330 templat.target = PIPE_TEXTURE_2D;
331 templat.format = formats[i];
332 templat.width0 = WIDTH;
333 templat.height0 = HEIGHT;
334 templat.depth0 = 1;
335 templat.last_level = 0;
336 templat.nr_samples = 1;
337 templat.bind = (PIPE_BIND_RENDER_TARGET |
338 PIPE_BIND_DISPLAY_TARGET);
339
340 rttex = screen->resource_create(screen,
341 &templat);
342 if (rttex == NULL)
343 exit(4);
344
345 surf = screen->get_tex_surface(screen, rttex, 0, 0, 0,
346 PIPE_BIND_RENDER_TARGET |
347 PIPE_BIND_DISPLAY_TARGET);
348 if (surf == NULL)
349 exit(5);
350
351 memset(&fb, 0, sizeof fb);
352 fb.nr_cbufs = 1;
353 fb.width = WIDTH;
354 fb.height = HEIGHT;
355 fb.cbufs[0] = surf;
356
357 ctx->set_framebuffer_state(ctx, &fb);
358
359 {
360 struct pipe_blend_state blend;
361 void *handle;
362 memset(&blend, 0, sizeof blend);
363 blend.rt[0].colormask = PIPE_MASK_RGBA;
364 handle = ctx->create_blend_state(ctx, &blend);
365 ctx->bind_blend_state(ctx, handle);
366 }
367
368 {
369 struct pipe_depth_stencil_alpha_state depthstencil;
370 void *handle;
371 memset(&depthstencil, 0, sizeof depthstencil);
372 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
373 ctx->bind_depth_stencil_alpha_state(ctx, handle);
374 }
375
376 {
377 struct pipe_rasterizer_state rasterizer;
378 void *handle;
379 memset(&rasterizer, 0, sizeof rasterizer);
380 rasterizer.cull_face = PIPE_FACE_NONE;
381 rasterizer.gl_rasterization_rules = 1;
382 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
383 ctx->bind_rasterizer_state(ctx, handle);
384 }
385
386 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
387
388 init_tex();
389
390 set_vertices();
391 set_vertex_shader();
392 set_fragment_shader();
393 }
394
395
396 int main( int argc, char *argv[] )
397 {
398 init();
399
400 graw_set_display_func( draw );
401 graw_main_loop();
402 return 0;
403 }