graw/tests: Use pipe_context::draw_vbo.
[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 "state_tracker/graw.h"
6 #include "pipe/p_screen.h"
7 #include "pipe/p_context.h"
8 #include "pipe/p_state.h"
9 #include "pipe/p_defines.h"
10
11 #include "util/u_debug.h" /* debug_dump_surface_bmp() */
12 #include "util/u_memory.h" /* Offset() */
13 #include "util/u_draw_quad.h"
14
15 enum pipe_format formats[] = {
16 PIPE_FORMAT_R8G8B8A8_UNORM,
17 PIPE_FORMAT_B8G8R8A8_UNORM,
18 PIPE_FORMAT_NONE
19 };
20
21 static const int WIDTH = 300;
22 static const int HEIGHT = 300;
23
24 static struct pipe_screen *screen = NULL;
25 static struct pipe_context *ctx = NULL;
26 static struct pipe_surface *surf = NULL;
27 static void *window = NULL;
28
29 struct vertex {
30 float position[4];
31 float color[4];
32 };
33
34 static struct vertex vertices[4] =
35 {
36 { { 0.0f, -0.9f, 0.0f, 1.0f },
37 { 1.0f, 0.0f, 0.0f, 1.0f }
38 },
39 { { -0.9f, 0.9f, 0.0f, 1.0f },
40 { 0.0f, 1.0f, 0.0f, 1.0f }
41 },
42 { { 0.9f, 0.9f, 0.0f, 1.0f },
43 { 0.0f, 0.0f, 1.0f, 1.0f }
44 }
45 };
46
47
48
49
50 static void set_viewport( float x, float y,
51 float width, float height,
52 float near, float far)
53 {
54 float z = far;
55 float half_width = (float)width / 2.0f;
56 float half_height = (float)height / 2.0f;
57 float half_depth = ((float)far - (float)near) / 2.0f;
58 struct pipe_viewport_state vp;
59
60 vp.scale[0] = half_width;
61 vp.scale[1] = half_height;
62 vp.scale[2] = half_depth;
63 vp.scale[3] = 1.0f;
64
65 vp.translate[0] = half_width + x;
66 vp.translate[1] = half_height + y;
67 vp.translate[2] = half_depth + z;
68 vp.translate[3] = 0.0f;
69
70 ctx->set_viewport_state( ctx, &vp );
71 }
72
73 static void set_vertices( void )
74 {
75 struct pipe_vertex_element ve[2];
76 struct pipe_vertex_buffer vbuf;
77 void *handle;
78
79 memset(ve, 0, sizeof ve);
80
81 ve[0].src_offset = Offset(struct vertex, position);
82 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
83 ve[1].src_offset = Offset(struct vertex, color);
84 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
85
86 handle = ctx->create_vertex_elements_state(ctx, 2, ve);
87 ctx->bind_vertex_elements_state(ctx, handle);
88
89
90 vbuf.stride = sizeof( struct vertex );
91 vbuf.max_index = sizeof(vertices) / vbuf.stride;
92 vbuf.buffer_offset = 0;
93 vbuf.buffer = screen->user_buffer_create(screen,
94 vertices,
95 sizeof(vertices),
96 PIPE_BIND_VERTEX_BUFFER);
97
98 ctx->set_vertex_buffers(ctx, 1, &vbuf);
99 }
100
101 static void set_vertex_shader( void )
102 {
103 void *handle;
104 const char *text =
105 "VERT\n"
106 "DCL IN[0]\n"
107 "DCL IN[1]\n"
108 "DCL OUT[0], POSITION\n"
109 "DCL OUT[1], COLOR\n"
110 " 0: MOV OUT[1], IN[1]\n"
111 " 1: MOV OUT[0], IN[0]\n"
112 " 2: END\n";
113
114 handle = graw_parse_vertex_shader(ctx, text);
115 ctx->bind_vs_state(ctx, handle);
116 }
117
118 static void set_fragment_shader( void )
119 {
120 void *handle;
121 const char *text =
122 "FRAG\n"
123 "DCL IN[0], COLOR, LINEAR\n"
124 "DCL OUT[0], COLOR\n"
125 " 0: MOV OUT[0], IN[0]\n"
126 " 1: END\n";
127
128 handle = graw_parse_fragment_shader(ctx, text);
129 ctx->bind_fs_state(ctx, handle);
130 }
131
132
133 static void draw( void )
134 {
135 float clear_color[4] = {1,0,1,1};
136
137 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
138 util_draw_arrays(ctx, PIPE_PRIM_TRIANGLES, 0, 3);
139 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
140
141 #if 0
142 /* At the moment, libgraw leaks out/makes available some of the
143 * symbols from gallium/auxiliary, including these debug helpers.
144 * Will eventually want to bless some of these paths, and lock the
145 * others down so they aren't accessible from test programs.
146 *
147 * This currently just happens to work on debug builds - a release
148 * build will probably fail to link here:
149 */
150 debug_dump_surface_bmp(ctx, "result.bmp", surf);
151 #endif
152
153 screen->flush_frontbuffer(screen, surf, window);
154 }
155
156
157 static void init( void )
158 {
159 struct pipe_framebuffer_state fb;
160 struct pipe_resource *tex, templat;
161 int i;
162
163 /* It's hard to say whether window or screen should be created
164 * first. Different environments would prefer one or the other.
165 *
166 * Also, no easy way of querying supported formats if the screen
167 * cannot be created first.
168 */
169 for (i = 0;
170 window == NULL && formats[i] != PIPE_FORMAT_NONE;
171 i++) {
172
173 screen = graw_create_window_and_screen(0,0,300,300,
174 formats[i],
175 &window);
176 }
177
178 ctx = screen->context_create(screen, NULL);
179 if (ctx == NULL)
180 exit(3);
181
182 templat.target = PIPE_TEXTURE_2D;
183 templat.format = formats[i];
184 templat.width0 = WIDTH;
185 templat.height0 = HEIGHT;
186 templat.depth0 = 1;
187 templat.last_level = 0;
188 templat.nr_samples = 1;
189 templat.bind = (PIPE_BIND_RENDER_TARGET |
190 PIPE_BIND_DISPLAY_TARGET);
191
192 tex = screen->resource_create(screen,
193 &templat);
194 if (tex == NULL)
195 exit(4);
196
197 surf = screen->get_tex_surface(screen, tex, 0, 0, 0,
198 PIPE_BIND_RENDER_TARGET |
199 PIPE_BIND_DISPLAY_TARGET);
200 if (surf == NULL)
201 exit(5);
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
245 int main( int argc, char *argv[] )
246 {
247 init();
248
249 graw_set_display_func( draw );
250 graw_main_loop();
251 return 0;
252 }