• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test the writing Z in fragment shader.
2  * The red quad should be entirely in front of the blue quad even
3  * though the overlap and intersect in Z.
4  */
5 
6 #include <stdio.h>
7 
8 #include "graw_util.h"
9 
10 #include "util/macros.h"
11 
12 
13 static int width = 300;
14 static int height = 300;
15 
16 static struct graw_info info;
17 
18 
19 struct vertex {
20    float position[4];
21    float color[4];
22 };
23 
24 #define z0 0.2
25 #define z01 0.5
26 #define z1 0.4
27 
28 
29 static struct vertex vertices[] =
30 {
31    /* left quad: clock-wise, front-facing, red */
32    {
33       {-0.8, -0.9, z0, 1.0 },
34       { 1, 0, 0, 1 }
35    },
36 
37    {
38       { -0.2, -0.9, z0, 1.0 },
39       { 1, 0, 0, 1 }
40    },
41 
42    {
43       { 0.2,  0.9, z01, 1.0 },
44       { 1, 0, 0, 1 }
45    },
46 
47    {
48       {-0.9,  0.9, z01, 1.0 },
49       { 1, 0, 0, 1 }
50    },
51 
52    /* right quad : counter-clock-wise, back-facing, green */
53    {
54       { 0.2,  -0.9, z1, 1.0 },
55       { 0, 0, 1, -1 }
56    },
57 
58    {
59       { -0.2,  0.8, z1, 1.0 },
60       { 0, 0, 1, -1 }
61    },
62 
63    {
64       { 0.9,  0.8, z1, 1.0 },
65       { 0, 0, 1, -1 }
66    },
67 
68    {
69       { 0.8, -0.9, z1, 1.0 },
70       { 0, 0, 1, -1 }
71    },
72 };
73 
74 #define NUM_VERTS ARRAY_SIZE(vertices)
75 
76 
77 
78 static void
set_vertices(void)79 set_vertices(void)
80 {
81    struct pipe_vertex_element ve[2];
82    struct pipe_vertex_buffer vbuf;
83    void *handle;
84 
85    memset(ve, 0, sizeof ve);
86 
87    ve[0].src_offset = Offset(struct vertex, position);
88    ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
89    ve[1].src_offset = Offset(struct vertex, color);
90    ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
91 
92    handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
93    info.ctx->bind_vertex_elements_state(info.ctx, handle);
94 
95    memset(&vbuf, 0, sizeof vbuf);
96 
97    vbuf.stride = sizeof(struct vertex);
98    vbuf.buffer_offset = 0;
99    vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
100                                               PIPE_BIND_VERTEX_BUFFER,
101                                               PIPE_USAGE_DEFAULT,
102                                               sizeof(vertices),
103                                               vertices);
104 
105    info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);
106 }
107 
108 
109 static void
set_vertex_shader(void)110 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[0], IN[0]\n"
120       "  1: MOV OUT[1], IN[1]\n"
121       "  2: END\n";
122 
123    handle = graw_parse_vertex_shader(info.ctx, text);
124    info.ctx->bind_vs_state(info.ctx, handle);
125 }
126 
127 
128 static void
set_fragment_shader(void)129 set_fragment_shader(void)
130 {
131    void *handle;
132    const char *text =
133       "FRAG\n"
134       "DCL IN[0], GENERIC, CONSTANT\n"
135       "DCL OUT[0], COLOR\n"
136       "DCL OUT[1], POSITION\n"
137       "DCL TEMP[0]\n"
138       "IMM FLT32 {    1.0,     0.0,     0.0,     0.0 }\n"
139       "IMM FLT32 {    0.0,     1.0,     0.0,     0.0 }\n"
140       "IMM FLT32 {    0.5,     0.4,     0.0,     0.0 }\n"
141       " 0: MOV OUT[0], IN[0]\n"    /* front-facing: red */
142       " 1: IF IN[0].xxxx :3\n"
143       " 2:   MOV OUT[1].z, IMM[2].yyyy\n"   /* red: Z = 0.4 */
144       " 3: ELSE :5\n"
145       " 4:   MOV OUT[1].z, IMM[2].xxxx\n"   /* blue: Z = 0.5 */
146       " 5: ENDIF\n"
147       " 6: END\n";
148 
149    handle = graw_parse_fragment_shader(info.ctx, text);
150    info.ctx->bind_fs_state(info.ctx, handle);
151 }
152 
153 
154 
155 static void
draw(void)156 draw(void)
157 {
158    union pipe_color_union clear_color;
159 
160    clear_color.f[0] = 0.25;
161    clear_color.f[1] = 0.25;
162    clear_color.f[2] = 0.25;
163    clear_color.f[3] = 1.00;
164 
165    info.ctx->clear(info.ctx,
166               PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,
167               NULL,
168               &clear_color, 1.0, 0);
169    util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);
170    info.ctx->flush(info.ctx, NULL, 0);
171 
172 #if 0
173    /* At the moment, libgraw leaks out/makes available some of the
174     * symbols from gallium/auxiliary, including these debug helpers.
175     * Will eventually want to bless some of these paths, and lock the
176     * others down so they aren't accessible from test programs.
177     *
178     * This currently just happens to work on debug builds - a release
179     * build will probably fail to link here:
180     */
181    debug_dump_surface_bmp(info.ctx, "result.bmp", surf);
182 #endif
183 
184    graw_util_flush_front(&info);
185 }
186 
187 
188 #if 0
189 static void
190 resize(int w, int h)
191 {
192    width = w;
193    height = h;
194 
195    graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
196 }
197 #endif
198 
199 
200 static void
init(void)201 init(void)
202 {
203    if (!graw_util_create_window(&info, width, height, 1, TRUE))
204       exit(1);
205 
206    graw_util_default_state(&info, TRUE);
207 
208    graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);
209 
210    set_vertices();
211    set_vertex_shader();
212    set_fragment_shader();
213 }
214 
215 
216 int
main(int argc,char * argv[])217 main(int argc, char *argv[])
218 {
219    init();
220 
221    printf("The red quad should be entirely in front of the blue quad.\n");
222 
223    graw_set_display_func(draw);
224    /*graw_set_reshape_func(resize);*/
225    graw_main_loop();
226    return 0;
227 }
228