13464ebd5Sriastradh
23464ebd5Sriastradh#include "pipe/p_compiler.h"
33464ebd5Sriastradh#include "pipe/p_context.h"
43464ebd5Sriastradh#include "pipe/p_shader_tokens.h"
53464ebd5Sriastradh#include "pipe/p_state.h"
63464ebd5Sriastradh#include "tgsi/tgsi_text.h"
73464ebd5Sriastradh#include "util/u_debug.h"
801e04c3fSmrg#include "util/u_debug_image.h"
93464ebd5Sriastradh#include "util/u_memory.h"
107ec681f3Smrg#include "frontend/graw.h"
113464ebd5Sriastradh
123464ebd5Sriastradh
133464ebd5Sriastradh/* Helper functions.  These are the same for all graw implementations.
143464ebd5Sriastradh */
153464ebd5SriastradhPUBLIC void *
163464ebd5Sriastradhgraw_parse_geometry_shader(struct pipe_context *pipe,
173464ebd5Sriastradh                           const char *text)
183464ebd5Sriastradh{
193464ebd5Sriastradh   struct tgsi_token tokens[1024];
203464ebd5Sriastradh   struct pipe_shader_state state;
213464ebd5Sriastradh
2201e04c3fSmrg   if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens)))
233464ebd5Sriastradh      return NULL;
243464ebd5Sriastradh
25af69d88dSmrg   memset(&state, 0, sizeof state);
263464ebd5Sriastradh   state.tokens = tokens;
273464ebd5Sriastradh   return pipe->create_gs_state(pipe, &state);
283464ebd5Sriastradh}
293464ebd5Sriastradh
303464ebd5SriastradhPUBLIC void *
313464ebd5Sriastradhgraw_parse_vertex_shader(struct pipe_context *pipe,
323464ebd5Sriastradh                         const char *text)
333464ebd5Sriastradh{
343464ebd5Sriastradh   struct tgsi_token tokens[1024];
353464ebd5Sriastradh   struct pipe_shader_state state;
363464ebd5Sriastradh
3701e04c3fSmrg   if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens)))
383464ebd5Sriastradh      return NULL;
393464ebd5Sriastradh
40af69d88dSmrg   memset(&state, 0, sizeof state);
413464ebd5Sriastradh   state.tokens = tokens;
423464ebd5Sriastradh   return pipe->create_vs_state(pipe, &state);
433464ebd5Sriastradh}
443464ebd5Sriastradh
453464ebd5SriastradhPUBLIC void *
463464ebd5Sriastradhgraw_parse_fragment_shader(struct pipe_context *pipe,
473464ebd5Sriastradh                           const char *text)
483464ebd5Sriastradh{
493464ebd5Sriastradh   struct tgsi_token tokens[1024];
503464ebd5Sriastradh   struct pipe_shader_state state;
513464ebd5Sriastradh
5201e04c3fSmrg   if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens)))
533464ebd5Sriastradh      return NULL;
543464ebd5Sriastradh
55af69d88dSmrg   memset(&state, 0, sizeof state);
563464ebd5Sriastradh   state.tokens = tokens;
573464ebd5Sriastradh   return pipe->create_fs_state(pipe, &state);
583464ebd5Sriastradh}
593464ebd5Sriastradh
603464ebd5Sriastradhstatic char out_filename[256] = "";
613464ebd5Sriastradh
627ec681f3SmrgPUBLIC bool
633464ebd5Sriastradhgraw_parse_args(int *argi,
643464ebd5Sriastradh                int argc,
653464ebd5Sriastradh                char *argv[])
663464ebd5Sriastradh{
673464ebd5Sriastradh   if (strcmp(argv[*argi], "-o") == 0) {
683464ebd5Sriastradh      if (*argi + 1 >= argc) {
697ec681f3Smrg         return false;
703464ebd5Sriastradh      }
713464ebd5Sriastradh
723464ebd5Sriastradh      strncpy(out_filename, argv[*argi + 1], sizeof(out_filename) - 1);
733464ebd5Sriastradh      out_filename[sizeof(out_filename) - 1] = '\0';
743464ebd5Sriastradh      *argi += 2;
757ec681f3Smrg      return true;
763464ebd5Sriastradh   }
773464ebd5Sriastradh
787ec681f3Smrg   return false;
793464ebd5Sriastradh}
803464ebd5Sriastradh
817ec681f3SmrgPUBLIC bool
823464ebd5Sriastradhgraw_save_surface_to_file(struct pipe_context *pipe,
833464ebd5Sriastradh                          struct pipe_surface *surface,
843464ebd5Sriastradh                          const char *filename)
853464ebd5Sriastradh{
863464ebd5Sriastradh   if (!filename || !*filename) {
873464ebd5Sriastradh      filename = out_filename;
883464ebd5Sriastradh      if (!filename || !*filename) {
897ec681f3Smrg         return false;
903464ebd5Sriastradh      }
913464ebd5Sriastradh   }
923464ebd5Sriastradh
933464ebd5Sriastradh   /* XXX: Make that working in release builds.
943464ebd5Sriastradh    */
953464ebd5Sriastradh   debug_dump_surface_bmp(pipe, filename, surface);
967ec681f3Smrg   return true;
973464ebd5Sriastradh}
98