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