Path: blob/21.2-virgl/src/gallium/tests/graw/fs-write-z.c
4565 views
/* Test the writing Z in fragment shader.1* The red quad should be entirely in front of the blue quad even2* though the overlap and intersect in Z.3*/45#include <stdio.h>67#include "graw_util.h"89#include "util/macros.h"101112static int width = 300;13static int height = 300;1415static struct graw_info info;161718struct vertex {19float position[4];20float color[4];21};2223#define z0 0.224#define z01 0.525#define z1 0.4262728static struct vertex vertices[] =29{30/* left quad: clock-wise, front-facing, red */31{32{-0.8, -0.9, z0, 1.0 },33{ 1, 0, 0, 1 }34},3536{37{ -0.2, -0.9, z0, 1.0 },38{ 1, 0, 0, 1 }39},4041{42{ 0.2, 0.9, z01, 1.0 },43{ 1, 0, 0, 1 }44},4546{47{-0.9, 0.9, z01, 1.0 },48{ 1, 0, 0, 1 }49},5051/* right quad : counter-clock-wise, back-facing, green */52{53{ 0.2, -0.9, z1, 1.0 },54{ 0, 0, 1, -1 }55},5657{58{ -0.2, 0.8, z1, 1.0 },59{ 0, 0, 1, -1 }60},6162{63{ 0.9, 0.8, z1, 1.0 },64{ 0, 0, 1, -1 }65},6667{68{ 0.8, -0.9, z1, 1.0 },69{ 0, 0, 1, -1 }70},71};7273#define NUM_VERTS ARRAY_SIZE(vertices)74757677static void78set_vertices(void)79{80struct pipe_vertex_element ve[2];81struct pipe_vertex_buffer vbuf;82void *handle;8384memset(ve, 0, sizeof ve);8586ve[0].src_offset = Offset(struct vertex, position);87ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;88ve[1].src_offset = Offset(struct vertex, color);89ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;9091handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);92info.ctx->bind_vertex_elements_state(info.ctx, handle);9394memset(&vbuf, 0, sizeof vbuf);9596vbuf.stride = sizeof(struct vertex);97vbuf.buffer_offset = 0;98vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,99PIPE_BIND_VERTEX_BUFFER,100PIPE_USAGE_DEFAULT,101sizeof(vertices),102vertices);103104info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);105}106107108static void109set_vertex_shader(void)110{111void *handle;112const char *text =113"VERT\n"114"DCL IN[0]\n"115"DCL IN[1]\n"116"DCL OUT[0], POSITION\n"117"DCL OUT[1], GENERIC[0]\n"118" 0: MOV OUT[0], IN[0]\n"119" 1: MOV OUT[1], IN[1]\n"120" 2: END\n";121122handle = graw_parse_vertex_shader(info.ctx, text);123info.ctx->bind_vs_state(info.ctx, handle);124}125126127static void128set_fragment_shader(void)129{130void *handle;131const char *text =132"FRAG\n"133"DCL IN[0], GENERIC, CONSTANT\n"134"DCL OUT[0], COLOR\n"135"DCL OUT[1], POSITION\n"136"DCL TEMP[0]\n"137"IMM FLT32 { 1.0, 0.0, 0.0, 0.0 }\n"138"IMM FLT32 { 0.0, 1.0, 0.0, 0.0 }\n"139"IMM FLT32 { 0.5, 0.4, 0.0, 0.0 }\n"140" 0: MOV OUT[0], IN[0]\n" /* front-facing: red */141" 1: IF IN[0].xxxx :3\n"142" 2: MOV OUT[1].z, IMM[2].yyyy\n" /* red: Z = 0.4 */143" 3: ELSE :5\n"144" 4: MOV OUT[1].z, IMM[2].xxxx\n" /* blue: Z = 0.5 */145" 5: ENDIF\n"146" 6: END\n";147148handle = graw_parse_fragment_shader(info.ctx, text);149info.ctx->bind_fs_state(info.ctx, handle);150}151152153154static void155draw(void)156{157union pipe_color_union clear_color;158159clear_color.f[0] = 0.25;160clear_color.f[1] = 0.25;161clear_color.f[2] = 0.25;162clear_color.f[3] = 1.00;163164info.ctx->clear(info.ctx,165PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,166NULL,167&clear_color, 1.0, 0);168util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);169info.ctx->flush(info.ctx, NULL, 0);170171#if 0172/* At the moment, libgraw leaks out/makes available some of the173* symbols from gallium/auxiliary, including these debug helpers.174* Will eventually want to bless some of these paths, and lock the175* others down so they aren't accessible from test programs.176*177* This currently just happens to work on debug builds - a release178* build will probably fail to link here:179*/180debug_dump_surface_bmp(info.ctx, "result.bmp", surf);181#endif182183graw_util_flush_front(&info);184}185186187#if 0188static void189resize(int w, int h)190{191width = w;192height = h;193194graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);195}196#endif197198199static void200init(void)201{202if (!graw_util_create_window(&info, width, height, 1, TRUE))203exit(1);204205graw_util_default_state(&info, TRUE);206207graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);208209set_vertices();210set_vertex_shader();211set_fragment_shader();212}213214215int216main(int argc, char *argv[])217{218init();219220printf("The red quad should be entirely in front of the blue quad.\n");221222graw_set_display_func(draw);223/*graw_set_reshape_func(resize);*/224graw_main_loop();225return 0;226}227228229