Path: blob/21.2-virgl/src/gallium/tests/graw/fs-frontface.c
4565 views
/* Test the TGSI_SEMANTIC_FACE fragment shader input.1*/23#include <stdio.h>45#include "graw_util.h"67#include "util/macros.h"8910static int width = 300;11static int height = 300;1213static struct graw_info info;1415struct vertex {16float position[4];17float color[4];18};1920#define z0 0.221#define z01 0.522#define z1 0.42324static struct vertex vertices[] =25{26/* left quad: clock-wise, front-facing, red */27{28{-0.8, -0.9, z0, 1.0 },29{ 0, 0, 0, 1 }30},3132{33{ -0.2, -0.9, z0, 1.0 },34{ 0, 0, 0, 1 }35},3637{38{ 0.2, 0.9, z01, 1.0 },39{ 0, 0, 0, 1 }40},4142{43{-0.9, 0.9, z01, 1.0 },44{ 0, 0, 0, 1 }45},4647/* right quad : counter-clock-wise, back-facing, green */48{49{ 0.2, -0.9, z1, 1.0 },50{ 1, 1, 1, -1 }51},5253{54{ -0.2, 0.8, z1, 1.0 },55{ 1, 1, 1, -1 }56},5758{59{ 0.9, 0.8, z1, 1.0 },60{ 1, 1, 1, -1 }61},6263{64{ 0.8, -0.9, z1, 1.0 },65{ 1, 1, 1, -1 }66},67};6869#define NUM_VERTS ARRAY_SIZE(vertices)70717273static void74set_vertices(void)75{76struct pipe_vertex_element ve[2];77struct pipe_vertex_buffer vbuf;78void *handle;7980memset(ve, 0, sizeof ve);8182ve[0].src_offset = Offset(struct vertex, position);83ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;84ve[1].src_offset = Offset(struct vertex, color);85ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;8687handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);88info.ctx->bind_vertex_elements_state(info.ctx, handle);8990memset(&vbuf, 0, sizeof vbuf);9192vbuf.stride = sizeof(struct vertex);93vbuf.buffer_offset = 0;94vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,95PIPE_BIND_VERTEX_BUFFER,96PIPE_USAGE_DEFAULT,97sizeof(vertices),98vertices);99100info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);101}102103104static void105set_vertex_shader(void)106{107void *handle;108const 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], GENERIC[0]\n"114" 0: MOV OUT[0], IN[0]\n"115" 1: MOV OUT[1], IN[1]\n"116" 2: END\n";117118handle = graw_parse_vertex_shader(info.ctx, text);119info.ctx->bind_vs_state(info.ctx, handle);120}121122123static void124set_fragment_shader(void)125{126void *handle;127const char *text =128"FRAG\n"129"DCL IN[0], FACE, CONSTANT\n"130"DCL IN[1], GENERIC, CONSTANT\n"131"DCL OUT[0], COLOR\n"132"DCL TEMP[0]\n"133"IMM FLT32 { 1.0, 0.0, 0.0, 0.0 }\n"134"IMM FLT32 { 0.0, 1.0, 0.0, 0.0 }\n"135"IMM FLT32 { 0.5, 0.6, 0.0, 0.0 }\n"136" 0: SGT TEMP[0].x, IN[0].xxxx, IMM[1].xxxx\n" /* TMP[0].x = IN[0].x > 0.0 */137" 1: IF TEMP[0].xxxx :4\n"138" 2: MOV OUT[0], IMM[0]\n" /* front-facing: red */139" 3: ELSE :5\n"140" 4: MOV OUT[0], IMM[1]\n" /* back-facing: green */141" 5: ENDIF\n"142" 6: END\n";143144handle = graw_parse_fragment_shader(info.ctx, text);145info.ctx->bind_fs_state(info.ctx, handle);146}147148149static void150draw(void)151{152union pipe_color_union clear_color;153154clear_color.f[0] = 0.25;155clear_color.f[1] = 0.25;156clear_color.f[2] = 0.25;157clear_color.f[3] = 1.00;158159info.ctx->clear(info.ctx,160PIPE_CLEAR_COLOR | PIPE_CLEAR_DEPTHSTENCIL,161NULL,162&clear_color, 1.0, 0);163util_draw_arrays(info.ctx, PIPE_PRIM_QUADS, 0, NUM_VERTS);164info.ctx->flush(info.ctx, NULL, 0);165166graw_util_flush_front(&info);167}168169170#if 0171static void172resize(int w, int h)173{174width = w;175height = h;176177set_viewport(0, 0, width, height, 30, 1000);178}179#endif180181182static void183init(void)184{185if (!graw_util_create_window(&info, width, height, 1, TRUE))186exit(1);187188graw_util_default_state(&info, TRUE);189190graw_util_viewport(&info, 0, 0, width, height, -1.0, 1.0);191192set_vertices();193set_vertex_shader();194set_fragment_shader();195}196197198int199main(int argc, char *argv[])200{201init();202203printf("Left quad: clock-wise, front-facing, red\n");204printf("Right quad: counter clock-wise, back-facing, green\n");205206graw_set_display_func(draw);207/*graw_set_reshape_func(resize);*/208graw_main_loop();209return 0;210}211212213