graw: Export graw_save_surface_to_file().
[mesa.git] / src / gallium / tests / graw / tri.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 <stdio.h>
6
7 #include "state_tracker/graw.h"
8 #include "pipe/p_screen.h"
9 #include "pipe/p_context.h"
10 #include "pipe/p_state.h"
11 #include "pipe/p_defines.h"
12
13 #include "util/u_memory.h" /* Offset() */
14 #include "util/u_draw_quad.h"
15
16 enum pipe_format formats[] = {
17 PIPE_FORMAT_R8G8B8A8_UNORM,
18 PIPE_FORMAT_B8G8R8A8_UNORM,
19 PIPE_FORMAT_NONE
20 };
21
22 static const int WIDTH = 300;
23 static const int HEIGHT = 300;
24
25 static struct pipe_screen *screen = NULL;
26 static struct pipe_context *ctx = NULL;
27 static struct pipe_surface *surf = NULL;
28 static void *window = NULL;
29
30 struct vertex {
31 float position[4];
32 float color[4];
33 };
34
35 static struct vertex vertices[3] =
36 {
37 {
38 { 0.0f, -0.9f, 0.0f, 1.0f },
39 { 1.0f, 0.0f, 0.0f, 1.0f }
40 },
41 {
42 { -0.9f, 0.9f, 0.0f, 1.0f },
43 { 0.0f, 1.0f, 0.0f, 1.0f }
44 },
45 {
46 { 0.9f, 0.9f, 0.0f, 1.0f },
47 { 0.0f, 0.0f, 1.0f, 1.0f }
48 }
49 };
50
51
52
53
54 static void set_viewport( float x, float y,
55 float width, float height,
56 float near, float far)
57 {
58 float z = far;
59 float half_width = (float)width / 2.0f;
60 float half_height = (float)height / 2.0f;
61 float half_depth = ((float)far - (float)near) / 2.0f;
62 struct pipe_viewport_state vp;
63
64 vp.scale[0] = half_width;
65 vp.scale[1] = half_height;
66 vp.scale[2] = half_depth;
67 vp.scale[3] = 1.0f;
68
69 vp.translate[0] = half_width + x;
70 vp.translate[1] = half_height + y;
71 vp.translate[2] = half_depth + z;
72 vp.translate[3] = 0.0f;
73
74 ctx->set_viewport_state( ctx, &vp );
75 }
76
77 static void set_vertices( void )
78 {
79 struct pipe_vertex_element ve[2];
80 struct pipe_vertex_buffer vbuf;
81 void *handle;
82
83 memset(ve, 0, sizeof ve);
84
85 ve[0].src_offset = Offset(struct vertex, position);
86 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
87 ve[1].src_offset = Offset(struct vertex, color);
88 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
89
90 handle = ctx->create_vertex_elements_state(ctx, 2, ve);
91 ctx->bind_vertex_elements_state(ctx, handle);
92
93
94 vbuf.stride = sizeof( struct vertex );
95 vbuf.max_index = sizeof(vertices) / vbuf.stride;
96 vbuf.buffer_offset = 0;
97 vbuf.buffer = screen->user_buffer_create(screen,
98 vertices,
99 sizeof(vertices),
100 PIPE_BIND_VERTEX_BUFFER);
101
102 ctx->set_vertex_buffers(ctx, 1, &vbuf);
103 }
104
105 static void set_vertex_shader( void )
106 {
107 void *handle;
108 const char *text =
109 "VERT\n"
110 "DCL IN[0]\n"
111 "DCL IN[1]\n"
112 "DCL OUT[0], POSITION\n"
113 "DCL OUT[1], COLOR\n"
114 " 0: MOV OUT[1], IN[1]\n"
115 " 1: MOV OUT[0], IN[0]\n"
116 " 2: END\n";
117
118 handle = graw_parse_vertex_shader(ctx, text);
119 ctx->bind_vs_state(ctx, handle);
120 }
121
122 static void set_fragment_shader( void )
123 {
124 void *handle;
125 const char *text =
126 "FRAG\n"
127 "DCL IN[0], COLOR, LINEAR\n"
128 "DCL OUT[0], COLOR\n"
129 " 0: MOV OUT[0], IN[0]\n"
130 " 1: END\n";
131
132 handle = graw_parse_fragment_shader(ctx, text);
133 ctx->bind_fs_state(ctx, handle);
134 }
135
136
137 static void draw( void )
138 {
139 float clear_color[4] = {1,0,1,1};
140
141 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
142 util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
143 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
144
145 graw_save_surface_to_file(ctx, surf, NULL);
146
147 screen->flush_frontbuffer(screen, surf, window);
148 }
149
150
151 static void init( void )
152 {
153 struct pipe_framebuffer_state fb;
154 struct pipe_resource *tex, templat;
155 int i;
156
157 /* It's hard to say whether window or screen should be created
158 * first. Different environments would prefer one or the other.
159 *
160 * Also, no easy way of querying supported formats if the screen
161 * cannot be created first.
162 */
163 for (i = 0;
164 window == NULL && formats[i] != PIPE_FORMAT_NONE;
165 i++) {
166
167 screen = graw_create_window_and_screen(0,0,300,300,
168 formats[i],
169 &window);
170 }
171
172 ctx = screen->context_create(screen, NULL);
173 if (ctx == NULL) {
174 fprintf(stderr, "Unable to create context!\n");
175 exit(3);
176 }
177
178 templat.target = PIPE_TEXTURE_2D;
179 templat.format = formats[i];
180 templat.width0 = WIDTH;
181 templat.height0 = HEIGHT;
182 templat.depth0 = 1;
183 templat.last_level = 0;
184 templat.nr_samples = 1;
185 templat.bind = (PIPE_BIND_RENDER_TARGET |
186 PIPE_BIND_DISPLAY_TARGET);
187
188 tex = screen->resource_create(screen,
189 &templat);
190 if (tex == NULL) {
191 fprintf(stderr, "Unable to create screen texture!\n");
192 exit(4);
193 }
194
195 surf = screen->get_tex_surface(screen, tex, 0, 0, 0,
196 PIPE_BIND_RENDER_TARGET |
197 PIPE_BIND_DISPLAY_TARGET);
198 if (surf == NULL) {
199 fprintf(stderr, "Unable to get tex surface!\n");
200 exit(5);
201 }
202
203 memset(&fb, 0, sizeof fb);
204 fb.nr_cbufs = 1;
205 fb.width = WIDTH;
206 fb.height = HEIGHT;
207 fb.cbufs[0] = surf;
208
209 ctx->set_framebuffer_state(ctx, &fb);
210
211 {
212 struct pipe_blend_state blend;
213 void *handle;
214 memset(&blend, 0, sizeof blend);
215 blend.rt[0].colormask = PIPE_MASK_RGBA;
216 handle = ctx->create_blend_state(ctx, &blend);
217 ctx->bind_blend_state(ctx, handle);
218 }
219
220 {
221 struct pipe_depth_stencil_alpha_state depthstencil;
222 void *handle;
223 memset(&depthstencil, 0, sizeof depthstencil);
224 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
225 ctx->bind_depth_stencil_alpha_state(ctx, handle);
226 }
227
228 {
229 struct pipe_rasterizer_state rasterizer;
230 void *handle;
231 memset(&rasterizer, 0, sizeof rasterizer);
232 rasterizer.cull_face = PIPE_FACE_NONE;
233 rasterizer.gl_rasterization_rules = 1;
234 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
235 ctx->bind_rasterizer_state(ctx, handle);
236 }
237
238 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
239 set_vertices();
240 set_vertex_shader();
241 set_fragment_shader();
242 }
243
244 static void args(int argc, char *argv[])
245 {
246 int i;
247
248 for (i = 1; i < argc;) {
249 if (graw_parse_args(&i, argc, argv)) {
250 continue;
251 }
252 exit(1);
253 }
254 }
255
256 int main( int argc, char *argv[] )
257 {
258 args(argc, argv);
259 init();
260
261 graw_set_display_func( draw );
262 graw_main_loop();
263 return 0;
264 }