Path: blob/21.2-virgl/src/gallium/tests/graw/vs-test.c
4565 views
/* Display a cleared blue window. This demo has no dependencies on1* any utility code, just the graw interface and gallium.2*/34#include "frontend/graw.h"5#include "pipe/p_screen.h"6#include "pipe/p_context.h"7#include "pipe/p_shader_tokens.h"8#include "pipe/p_state.h"9#include "pipe/p_defines.h"1011#include <stdio.h> /* for fread(), etc */1213#include "util/u_inlines.h"14#include "util/u_memory.h" /* Offset() */15#include "util/u_draw_quad.h"16#include "util/u_box.h"1718static const char *filename = NULL;19unsigned show_fps = 0;202122static void usage(char *name)23{24fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);25#ifndef _WIN3226fprintf(stderr, "\n" );27fprintf(stderr, "options:\n");28fprintf(stderr, " -fps show frames per second\n");29#endif30}313233enum pipe_format formats[] = {34PIPE_FORMAT_RGBA8888_UNORM,35PIPE_FORMAT_BGRA8888_UNORM,36PIPE_FORMAT_NONE37};3839static const int WIDTH = 250;40static const int HEIGHT = 250;4142static struct pipe_screen *screen = NULL;43static struct pipe_context *ctx = NULL;44static struct pipe_resource *rttex = NULL;45static struct pipe_resource *constbuf = NULL;46static struct pipe_surface *surf = NULL;47static struct pipe_sampler_view *sv = NULL;48static void *sampler = NULL;49static void *window = NULL;50static struct pipe_resource *samptex = NULL;5152struct vertex {53float position[4];54float color[3];55};5657/* Draw a regular mesh58*/59#define MESH_SZ 1660static struct vertex vertices[MESH_SZ * MESH_SZ];6162static float constants[] =63{ 0.4, 0, 0, 1,641, 1, 1, 1,652, 2, 2, 2,664, 8, 16, 32,67683, 0, 0, 0,690, .5, 0, 0,700, 0, 1, 0,710, 0, 0, 1,72731, 0, 0, 0.5,740, 1, 0, 0.5,750, 0, 1, 0,760, 0, 0, 1,77};7879static void init_fs_constbuf( void )80{81struct pipe_resource templat;82struct pipe_box box;8384memset(&templat, 0, sizeof(templat));85templat.target = PIPE_BUFFER;86templat.format = PIPE_FORMAT_R8_UNORM;87templat.width0 = sizeof(constants);88templat.height0 = 1;89templat.depth0 = 1;90templat.array_size = 1;91templat.last_level = 0;92templat.bind = PIPE_BIND_CONSTANT_BUFFER;9394constbuf = screen->resource_create(screen,95&templat);96if (constbuf == NULL)97exit(4);9899100u_box_2d(0,0,sizeof(constants),1, &box);101102ctx->buffer_subdata(ctx, constbuf,103PIPE_MAP_WRITE,1040, sizeof(constants), constants);105106pipe_set_constant_buffer(ctx,107PIPE_SHADER_VERTEX, 0,108constbuf);109}110111112static void set_viewport( float x, float y,113float width, float height,114float zNear, float zFar)115{116float z = zFar;117float half_width = (float)width / 2.0f;118float half_height = (float)height / 2.0f;119float half_depth = ((float)zFar - (float)zNear) / 2.0f;120struct pipe_viewport_state vp;121122vp.scale[0] = half_width;123vp.scale[1] = half_height;124vp.scale[2] = half_depth;125126vp.translate[0] = half_width + x;127vp.translate[1] = half_height + y;128vp.translate[2] = half_depth + z;129130vp.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;131vp.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;132vp.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;133vp.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;134135ctx->set_viewport_states( ctx, 0, 1, &vp );136}137138static void set_vertices( void )139{140struct pipe_vertex_element ve[2];141struct pipe_vertex_buffer vbuf;142void *handle;143int x,y;144145memset(ve, 0, sizeof ve);146147ve[0].src_offset = Offset(struct vertex, position);148ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;149ve[1].src_offset = Offset(struct vertex, color);150ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;151152handle = ctx->create_vertex_elements_state(ctx, 2, ve);153ctx->bind_vertex_elements_state(ctx, handle);154155for (x = 0; x < MESH_SZ; x++) {156for (y = 0; y < MESH_SZ; y++) {157int i = y * MESH_SZ + x;158vertices[i].position[0] = ((float)x)/MESH_SZ * 2.0 - 1.0;159vertices[i].position[1] = ((float)y)/MESH_SZ * 2.0 - 1.0;160vertices[i].position[2] = 0;161vertices[i].position[3] = 1.0;162163vertices[i].color[0] = .5;164vertices[i].color[1] = (float)x / (float)MESH_SZ;165vertices[i].color[2] = (float)y / (float)MESH_SZ;166}167}168169memset(&vbuf, 0, sizeof vbuf);170171vbuf.stride = sizeof( struct vertex );172vbuf.buffer_offset = 0;173vbuf.buffer.resource = pipe_buffer_create_with_data(ctx,174PIPE_BIND_VERTEX_BUFFER,175PIPE_USAGE_DEFAULT,176sizeof(vertices),177vertices);178179ctx->set_vertex_buffers(ctx, 0, 1, 0, false, &vbuf);180}181182static void set_vertex_shader( void )183{184FILE *f;185char buf[50000];186void *handle;187int sz;188189if ((f = fopen(filename, "r")) == NULL) {190fprintf(stderr, "Couldn't open %s\n", filename);191exit(1);192}193194sz = fread(buf, 1, sizeof(buf), f);195if (!feof(f)) {196printf("file too long\n");197exit(1);198}199printf("%.*s\n", sz, buf);200buf[sz] = 0;201202handle = graw_parse_vertex_shader(ctx, buf);203ctx->bind_vs_state(ctx, handle);204fclose(f);205}206207static void set_fragment_shader( void )208{209void *handle;210const char *text =211"FRAG\n"212"DCL IN[0], COLOR, LINEAR\n"213"DCL OUT[0], COLOR\n"214" 0: MOV OUT[0], IN[0]\n"215" 1: END\n";216217handle = graw_parse_fragment_shader(ctx, text);218ctx->bind_fs_state(ctx, handle);219}220221222223static void draw( void )224{225union pipe_color_union clear_color = { {.1,.3,.5,0} };226227ctx->clear(ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);228util_draw_arrays(ctx, PIPE_PRIM_POINTS, 0, ARRAY_SIZE(vertices));229ctx->flush(ctx, NULL, 0);230231graw_save_surface_to_file(ctx, surf, NULL);232233screen->flush_frontbuffer(screen, ctx, rttex, 0, 0, window, NULL);234}235236#define SIZE 16237238static void init_tex( void )239{240struct pipe_sampler_view sv_template;241struct pipe_sampler_state sampler_desc;242struct pipe_resource templat;243struct pipe_box box;244ubyte tex2d[SIZE][SIZE][4];245int s, t;246247#if (SIZE != 2)248for (s = 0; s < SIZE; s++) {249for (t = 0; t < SIZE; t++) {250if (0) {251int x = (s ^ t) & 1;252tex2d[t][s][0] = (x) ? 0 : 63;253tex2d[t][s][1] = (x) ? 0 : 128;254tex2d[t][s][2] = 0;255tex2d[t][s][3] = 0xff;256}257else {258int x = ((s ^ t) >> 2) & 1;259tex2d[t][s][0] = s*255/(SIZE-1);260tex2d[t][s][1] = t*255/(SIZE-1);261tex2d[t][s][2] = (x) ? 0 : 128;262tex2d[t][s][3] = 0xff;263}264}265}266#else267tex2d[0][0][0] = 0;268tex2d[0][0][1] = 255;269tex2d[0][0][2] = 255;270tex2d[0][0][3] = 0;271272tex2d[0][1][0] = 0;273tex2d[0][1][1] = 0;274tex2d[0][1][2] = 255;275tex2d[0][1][3] = 255;276277tex2d[1][0][0] = 255;278tex2d[1][0][1] = 255;279tex2d[1][0][2] = 0;280tex2d[1][0][3] = 255;281282tex2d[1][1][0] = 255;283tex2d[1][1][1] = 0;284tex2d[1][1][2] = 0;285tex2d[1][1][3] = 255;286#endif287288memset(&templat, 0, sizeof(templat));289templat.target = PIPE_TEXTURE_2D;290templat.format = PIPE_FORMAT_B8G8R8A8_UNORM;291templat.width0 = SIZE;292templat.height0 = SIZE;293templat.depth0 = 1;294templat.array_size = 1;295templat.last_level = 0;296templat.bind = PIPE_BIND_SAMPLER_VIEW;297298299samptex = screen->resource_create(screen,300&templat);301if (samptex == NULL)302exit(4);303304u_box_2d(0,0,SIZE,SIZE, &box);305306ctx->texture_subdata(ctx,307samptex,3080,309PIPE_MAP_WRITE,310&box,311tex2d,312sizeof tex2d[0],313sizeof tex2d);314315/* Possibly read back & compare against original data:316*/317if (0)318{319struct pipe_transfer *t;320uint32_t *ptr;321ptr = pipe_texture_map(ctx, samptex,3220, 0, /* level, layer */323PIPE_MAP_READ,3240, 0, SIZE, SIZE, &t); /* x, y, width, height */325326if (memcmp(ptr, tex2d, sizeof tex2d) != 0) {327assert(0);328exit(9);329}330331ctx->texture_unmap(ctx, t);332}333334memset(&sv_template, 0, sizeof sv_template);335sv_template.format = samptex->format;336sv_template.texture = samptex;337sv_template.swizzle_r = 0;338sv_template.swizzle_g = 1;339sv_template.swizzle_b = 2;340sv_template.swizzle_a = 3;341sv = ctx->create_sampler_view(ctx, samptex, &sv_template);342if (sv == NULL)343exit(5);344345ctx->set_sampler_views(ctx, PIPE_SHADER_FRAGMENT, 0, 1, 0, &sv);346347348memset(&sampler_desc, 0, sizeof sampler_desc);349sampler_desc.wrap_s = PIPE_TEX_WRAP_REPEAT;350sampler_desc.wrap_t = PIPE_TEX_WRAP_REPEAT;351sampler_desc.wrap_r = PIPE_TEX_WRAP_REPEAT;352sampler_desc.min_img_filter = PIPE_TEX_FILTER_NEAREST;353sampler_desc.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;354sampler_desc.mag_img_filter = PIPE_TEX_FILTER_NEAREST;355sampler_desc.compare_mode = PIPE_TEX_COMPARE_NONE;356sampler_desc.compare_func = 0;357sampler_desc.normalized_coords = 1;358sampler_desc.max_anisotropy = 0;359360sampler = ctx->create_sampler_state(ctx, &sampler_desc);361if (sampler == NULL)362exit(6);363364ctx->bind_sampler_states(ctx, PIPE_SHADER_FRAGMENT, 0, 1, &sampler);365366}367368static void init( void )369{370struct pipe_framebuffer_state fb;371struct pipe_resource templat;372struct pipe_surface surf_tmpl;373int i;374375/* It's hard to say whether window or screen should be created376* first. Different environments would prefer one or the other.377*378* Also, no easy way of querying supported formats if the screen379* cannot be created first.380*/381for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {382screen = graw_create_window_and_screen(0, 0, 300, 300,383formats[i],384&window);385if (window && screen)386break;387}388if (!screen || !window) {389fprintf(stderr, "Unable to create window\n");390exit(1);391}392393ctx = screen->context_create(screen, NULL, 0);394if (ctx == NULL)395exit(3);396397memset(&templat, 0, sizeof(templat));398templat.target = PIPE_TEXTURE_2D;399templat.format = formats[i];400templat.width0 = WIDTH;401templat.height0 = HEIGHT;402templat.depth0 = 1;403templat.array_size = 1;404templat.last_level = 0;405templat.bind = (PIPE_BIND_RENDER_TARGET |406PIPE_BIND_DISPLAY_TARGET);407408rttex = screen->resource_create(screen,409&templat);410if (rttex == NULL)411exit(4);412413surf_tmpl.format = templat.format;414surf_tmpl.u.tex.level = 0;415surf_tmpl.u.tex.first_layer = 0;416surf_tmpl.u.tex.last_layer = 0;417surf = ctx->create_surface(ctx, rttex, &surf_tmpl);418if (surf == NULL)419exit(5);420421memset(&fb, 0, sizeof fb);422fb.nr_cbufs = 1;423fb.width = WIDTH;424fb.height = HEIGHT;425fb.cbufs[0] = surf;426427ctx->set_framebuffer_state(ctx, &fb);428429{430struct pipe_blend_state blend;431void *handle;432memset(&blend, 0, sizeof blend);433blend.rt[0].colormask = PIPE_MASK_RGBA;434handle = ctx->create_blend_state(ctx, &blend);435ctx->bind_blend_state(ctx, handle);436}437438{439struct pipe_depth_stencil_alpha_state depthstencil;440void *handle;441memset(&depthstencil, 0, sizeof depthstencil);442handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);443ctx->bind_depth_stencil_alpha_state(ctx, handle);444}445446{447struct pipe_rasterizer_state rasterizer;448void *handle;449memset(&rasterizer, 0, sizeof rasterizer);450rasterizer.cull_face = PIPE_FACE_NONE;451rasterizer.point_size = 8.0;452rasterizer.half_pixel_center = 1;453rasterizer.bottom_edge_rule = 1;454rasterizer.depth_clip_near = 1;455rasterizer.depth_clip_far = 1;456handle = ctx->create_rasterizer_state(ctx, &rasterizer);457ctx->bind_rasterizer_state(ctx, handle);458}459460set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);461462init_tex();463init_fs_constbuf();464465set_vertices();466set_vertex_shader();467set_fragment_shader();468}469470static void args(int argc, char *argv[])471{472int i;473474for (i = 1; i < argc;) {475if (graw_parse_args(&i, argc, argv)) {476continue;477}478if (strcmp(argv[i], "-fps") == 0) {479show_fps = 1;480i++;481}482else if (i == argc - 1) {483filename = argv[i];484i++;485}486else {487usage(argv[0]);488exit(1);489}490}491492if (!filename) {493usage(argv[0]);494exit(1);495}496}497498int main( int argc, char *argv[] )499{500args(argc,argv);501init();502503graw_set_display_func( draw );504graw_main_loop();505return 0;506}507508509