Example #1
0
void compile_all(const char *src, size_t src_size) {
  cl_uint i;

  // Get the platform list
  cl_platform_id *platforms = NULL;
  cl_uint num_platforms = 0;
  if (get_platform_list(&platforms, &num_platforms) != CL_SUCCESS) {
    return;
  }

  // For each platform compile binaries for each devices
  for (i = 0; i < num_platforms; ++i) {
    // Compile for each devices
    cl_uint num_devices = 0;
    cl_int err = compile_program(&num_devices, src, src_size, platforms[i], i);

    // Print the result
    char *platform_name = get_platform_info(platforms[i], CL_PLATFORM_NAME);
    printf("PLATFORM [%s]  -->  %s (%u)\n",
           (platform_name ? platform_name : ""),
           ((err == CL_SUCCESS) ? "SUCCESS" : "FAILURE"),
           (unsigned)num_devices);
    fflush(stdout);
    free(platform_name);
  }

  // Free the platform list
  free_platform_list(platforms, num_platforms);
}
Example #2
0
static bool compile_programs(GLuint *gl_prog)
{
   unsigned i;
   for (i = 0; i < glsl_shader->passes; i++)
   {
      struct gfx_shader_pass *pass = &glsl_shader->pass[i];

      // If we load from GLSLP (CGP),
      // load the file here, and pretend
      // we were really using XML all along.
      if (*pass->source.cg && !load_source_path(pass, pass->source.cg))
      {
         RARCH_ERR("Failed to load GLSL shader: %s.\n", pass->source.cg);
         return false;
      }
      *pass->source.cg = '\0';

      const char *vertex   = pass->source.xml.vertex;
      const char *fragment = pass->source.xml.fragment;

      gl_prog[i] = compile_program(vertex, fragment, i);

      if (!gl_prog[i])
      {
         RARCH_ERR("Failed to create GL program #%u.\n", i);
         return false;
      }
   }

   return true;
}
 /** @brief Returns the corresponding program. Compiles the operation if not previously done */
 viennacl::ocl::program & program(){
   init();
   std::string program_name_ = operations_manager_.repr();
   if(!viennacl::ocl::current_context().has_program(program_name_)){
     compile_program(program_name_);
   }
   return viennacl::ocl::current_context().get_program(program_name_);
 }
Example #4
0
struct glpal_ctx *pal_init_glsl(GLboolean float_packed_pixels)
{CHECK_GL_ERR;
	printf("Compiling pallet shader:\n");
	GLint prog = 0;
	if(!float_packed_pixels)
		prog = compile_program(vtx_shader, pal_frag_shader);
	else
		prog = compile_program(vtx_shader, pal_frag_mix);

	if(!prog) return NULL;

	glUseProgramObjectARB(prog);
	glUniform1iARB(glGetUniformLocationARB(prog, "src"), 0);
	glUniform1iARB(glGetUniformLocationARB(prog, "pal"), 1);
	glBindAttribLocationARB(prog, 0, "vertex");
	glUseProgramObjectARB(0);
	printf("Pallet shader compiled\n");

	struct priv_ctx *priv = malloc(sizeof(*priv));
	priv->pubctx.render = render;
	priv->pubctx.step = pal_step;
	priv->pubctx.start_switch = start_switch;
	priv->pubctx.changing = changing;
	priv->pal = pal_ctx_new(0);
	priv->prog = prog;
	priv->cnt = 0;

	glGenTextures(2, &priv->tex);
	glBindTexture(GL_TEXTURE_1D, priv->tex[0]);
	glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, pal_ctx_get_active(priv->pal));
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glBindTexture(GL_TEXTURE_1D, 0);

	glBindTexture(GL_TEXTURE_1D, priv->tex[1]);
	glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, pal_ctx_get_active(priv->pal));
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glBindTexture(GL_TEXTURE_1D, 0);

	CHECK_GL_ERR;
	return (struct glpal_ctx *)priv;
}
Example #5
0
struct glpal_ctx * pal_init_glsl(GLboolean float_packed_pixels)
{CHECK_GL_ERR;
	printf("Compiling pallet shader:\n");
	GLint prog = 0;
	if(!float_packed_pixels)
		prog = compile_program(vtx_shader, pal_frag_shader);
	else
		prog = compile_program(vtx_shader, pal_frag_mix);

	if(!prog) return NULL;

	struct pal_lst *pals = pallet_get_palettes();
	struct priv_ctx *priv = malloc(sizeof(*priv) + sizeof(*priv->pal_tex)*pals->numpals);
	priv->pubctx.render = render;
	priv->pubctx.step = step;
	priv->pubctx.start_switch = start_switch;
	priv->pubctx.changing = changing;
	priv->prog = prog;
	priv->numpal = pals->numpals;
	priv->curpal = priv->nextpal = 0;
	priv->palpos = 0;

	glUseProgramObjectARB(prog);
	glUniform1iARB(glGetUniformLocationARB(prog, "src"), 0);
	glUniform1iARB(glGetUniformLocationARB(prog, "pal1"), 1);
	glUniform1iARB(glGetUniformLocationARB(prog, "pal2"), 2);
	priv->palpos_loc = glGetUniformLocationARB(prog, "palpos");
	glUniform1fARB(priv->palpos_loc, 0.0f);
	glUseProgramObjectARB(0);
	printf("Pallet shader compiled\n");

	glGenTextures(pals->numpals, priv->pal_tex);

	glPushAttrib(GL_TEXTURE_BIT);
	for(int i=0; i<pals->numpals; i++) {
		glBindTexture(GL_TEXTURE_1D, priv->pal_tex[i]);
		glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, pals->pallets[i]);
		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}
	glPopAttrib(); CHECK_GL_ERR;
	free(pals);
	return (struct glpal_ctx *)priv;
}
Example #6
0
void chunk_init(Chunk *chunk, Vector3i *pos)
{
  if(buffer_regen == 0) {
    GLuint shaders[2];
    shaders[0] = load_shader("res/shader/chunk_regen.vert", GL_VERTEX_SHADER);
    shaders[1] = load_shader("res/shader/chunk_regen.geom", GL_GEOMETRY_SHADER);
    program_regen = compile_program(2, shaders);
    char *varyings[3] = {"pos", "texcoords", "tex"};
    set_xfb_varyings(program_regen, 3, varyings, GL_INTERLEAVED_ATTRIBS);

    shaders[0] = load_shader("res/shader/chunk_render.vert", GL_VERTEX_SHADER);
    shaders[1] = load_shader("res/shader/chunk_render.frag", GL_FRAGMENT_SHADER);
    program_render = compile_program(2, shaders);
    
    glGenBuffers(1, &buffer_regen);
    glBindBuffer(GL_ARRAY_BUFFER, buffer_regen);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*4096, NULL, GL_STATIC_DRAW);
    GLfloat *data = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
    for(int x=0;x<16;++x) {
      for(int y=0;y<16;++y) {
	for(int z=0;z<16;++z) {
	  int index = (x + (y * 16) + (z * 256)) * 3;
	  data[index] = (GLfloat) x;
	  data[index+1] = (GLfloat) y;
	  data[index+2] = (GLfloat) z;
	}
      }
    }
    glUnmapBuffer(GL_ARRAY_BUFFER);

    glGenVertexArrays(1, &vao_regen);
    glGenVertexArrays(1, &vao_render);
  }
  
  chunk->blocks = calloc(4096, sizeof(Block *));
  vec_copy3i(&chunk->pos, pos);
  chunk->changed = true;
  chunk->empty = true;
  glGenBuffers(1, &chunk->buffer);
  chunk->num_verts = 0;
}
Example #7
0
int set_shaders() {
    if (compile_program(&OGLAttr::geometry_shader.program, GEOMETRY_VERTEX_SHADER, GEOMETRY_FRAG_SHADER))
        return 1;
    if (bind_geometry_shader_attributes(OGLAttr::geometry_shader.program))
        return 1;
    
    if (compile_program(&OGLAttr::lighting_shader.program, LIGHTING_VERTEX_SHADER, LIGHTING_FRAG_SHADER))
        return 1;
    if (bind_light_shader_attributes(OGLAttr::lighting_shader.program))
        return 1;
    
    // TODO doing this means we dont call glUseProgram correctly. fix this!
    if (compile_program(&OGLAttr::shadow_shader.program, SHADOW_VERTEX_SHADER, SHADOW_FRAG_SHADER))
        return 1;
    if (bind_shadow_shader_attributes(OGLAttr::shadow_shader.program))
        return 1;
    
    glGenVertexArrays(1, &OGLAttr::vao);
    glBindVertexArray(OGLAttr::vao);
    return 0;
}
Example #8
0
int wam_consult(wam_t *wam, char *filename)
{
	char fn2[256];
	strcpy(fn2, filename+1);
	fn2[strlen(fn2)-1] = 0;
	prog_t *prog = compile_program(fn2);
	if (prog == NULL)
		wam_backtrack(wam);
	else {
		prog_add_prog(wam->prog, prog);
		prog_update_label(wam->prog);
		wam->pc += 1;
	}
	return 0;
}
Example #9
0
static bool compile_programs(glsl_shader_data_t *glsl, GLuint *gl_prog)
{
    unsigned i;

    for (i = 0; i < glsl->shader->passes; i++)
    {
        const char *vertex           = NULL;
        const char *fragment         = NULL;
        struct video_shader_pass *pass = (struct video_shader_pass*)
                                         &glsl->shader->pass[i];

        /* If we load from GLSLP (CGP),
         * load the file here, and pretend
         * we were really using XML all along.
         */
        if (*pass->source.path && !load_source_path(pass, pass->source.path))
        {
            RARCH_ERR("Failed to load GLSL shader: %s.\n", pass->source.path);
            return false;
        }
        *pass->source.path = '\0';

        vertex   = pass->source.string.vertex;
        fragment = pass->source.string.fragment;

        gl_prog[i] = compile_program(glsl, vertex, fragment, i);

        if (!gl_prog[i])
        {
            RARCH_ERR("Failed to create GL program #%u.\n", i);
            return false;
        }
    }

    return true;
}
Example #10
0
int main(int argc, char *argv[])
{
     int *grid;
     int *used;
     long count;
     int i, benchmark, help;
     //Cilk_time tm_begin, tm_elapsed;
    // Cilk_time wk_begin, wk_elapsed;
     //Cilk_time cp_begin, cp_elapsed;

     /* standard benchmark options */
     n = 4;

     get_options(argc, argv, specifiers, opt_types, &n, &benchmark, &help);

     if (help)
	  return usage();

     if (benchmark) {
	  switch (benchmark) {
	      case 1:		/* short benchmark options -- a little work */
		   n = 2;
		   break;
	      case 2:		/* standard benchmark options */
		   n = 4;
		   break;
	      case 3:		/* long benchmark options -- a lot of work */
		   n = 5;
		   break;
	  }
     }
     if (n <= 0) {
	  fprintf(stderr, "error: n must be positive\n");
	  return 1;
     }
     nsquare = n * n;
     magic_sum = n * (nsquare + 1) / 2;

     grid = (int *) malloc(nsquare * sizeof(int));
     used = (int *) malloc(nsquare * sizeof(int));
     for (i = 0; i < nsquare; ++i)
	  used[i] = grid[i] = 0;

     /* Timing. "Start" timers */
     sync;
     //cp_begin = Cilk_user_critical_path;
     //wk_begin = Cilk_user_work;
     //tm_begin = Cilk_get_wall_time();

     compile_program(program);
     count = spawn execute(grid, used, program, 1);
     sync;

     /* Timing. "Stop" timers */
     //tm_elapsed = Cilk_get_wall_time() - tm_begin;
     //wk_elapsed = Cilk_user_work - wk_begin;
     //cp_elapsed = Cilk_user_critical_path - cp_begin;

     printf("\nCilk Example: magic\n");
     //printf("	      running on %d processor%s\n\n", Cilk_active_size, Cilk_active_size > 1 ? "s" : "");
     printf("options: <n> = %d\n\n", n);
     printf("Result is %ld\n\n", count);
     //printf("Running time  = %4f s\n", Cilk_wall_time_to_sec(tm_elapsed));
     //printf("Work          = %4f s\n", Cilk_time_to_sec(wk_elapsed));
    // printf("Critical path = %4f s\n\n", Cilk_time_to_sec(cp_elapsed));

     return 0;
}
Example #11
0
static bool gl_glsl_init(void *data, const char *path)
{
   unsigned i;
   (void)data;
#ifndef HAVE_OPENGLES2
   RARCH_LOG("Checking GLSL shader support ...\n");
   bool shader_support = glCreateProgram && glUseProgram && glCreateShader
      && glDeleteShader && glShaderSource && glCompileShader && glAttachShader
      && glDetachShader && glLinkProgram && glGetUniformLocation
      && glUniform1i && glUniform1f && glUniform2fv && glUniform4fv && glUniformMatrix4fv
      && glGetShaderiv && glGetShaderInfoLog && glGetProgramiv && glGetProgramInfoLog 
      && glDeleteProgram && glGetAttachedShaders
      && glGetAttribLocation && glEnableVertexAttribArray && glDisableVertexAttribArray
      && glVertexAttribPointer
      && glGenBuffers && glBufferData && glDeleteBuffers && glBindBuffer;

   if (!shader_support)
   {
      RARCH_ERR("GLSL shaders aren't supported by your OpenGL driver.\n");
      return false;
   }
#endif

   glsl_shader = (struct gfx_shader*)calloc(1, sizeof(*glsl_shader));
   if (!glsl_shader)
      return false;

   if (path)
   {
      bool ret;
      if (strcmp(path_get_extension(path), "glsl") == 0)
      {
         strlcpy(glsl_shader->pass[0].source.cg, path, sizeof(glsl_shader->pass[0].source.cg));
         glsl_shader->passes = 1;
         glsl_shader->modern = true;
         ret = true;
      }
      else if (strcmp(path_get_extension(path), "glslp") == 0)
      {
         config_file_t *conf = config_file_new(path);
         if (conf)
         {
            ret = gfx_shader_read_conf_cgp(conf, glsl_shader);
            glsl_shader->modern = true;
            config_file_free(conf);
         }
         else
            ret = false;
      }
      else
         ret = gfx_shader_read_xml(path, glsl_shader);

      if (!ret)
      {
         RARCH_ERR("[GL]: Failed to parse GLSL shader.\n");
         return false;
      }
   }
   else
   {
      RARCH_WARN("[GL]: Stock GLSL shaders will be used.\n");
      glsl_shader->passes = 1;
      glsl_shader->pass[0].source.xml.vertex   = strdup(glsl_core ? stock_vertex_core : stock_vertex_modern);
      glsl_shader->pass[0].source.xml.fragment = strdup(glsl_core ? stock_fragment_core : stock_fragment_modern);
      glsl_shader->modern = true;
   }

   gfx_shader_resolve_relative(glsl_shader, path);

   const char *stock_vertex = glsl_shader->modern ?
      stock_vertex_modern : stock_vertex_legacy;
   const char *stock_fragment = glsl_shader->modern ?
      stock_fragment_modern : stock_fragment_legacy;

   if (glsl_core)
   {
      stock_vertex = stock_vertex_core;
      stock_fragment = stock_fragment_core;
   }

#ifdef HAVE_OPENGLES2
   if (!glsl_shader->modern)
   {
      RARCH_ERR("[GL]: GLES context is used, but shader is not modern. Cannot use it.\n");
      goto error;
   }
#else
   if (glsl_core && !glsl_shader->modern)
   {
      RARCH_ERR("[GL]: GL core context is used, but shader is not core compatible. Cannot use it.\n");
      goto error;
   }
#endif

   if (!(gl_program[0] = compile_program(stock_vertex, stock_fragment, 0)))
   {
      RARCH_ERR("GLSL stock programs failed to compile.\n");
      goto error;
   }

   if (!compile_programs(&gl_program[1]))
      goto error;

   if (!gl_load_luts(glsl_shader, gl_teximage))
   {
      RARCH_ERR("[GL]: Failed to load LUTs.\n");
      goto error;
   }

   for (i = 0; i <= glsl_shader->passes; i++)
      find_uniforms(i, gl_program[i], &gl_uniforms[i]);

#ifdef GLSL_DEBUG
   if (!gl_check_error())
      RARCH_WARN("Detected GL error in GLSL.\n");
#endif

   if (glsl_shader->variables)
   {
      struct state_tracker_info info = {0};
      info.wram      = (uint8_t*)pretro_get_memory_data(RETRO_MEMORY_SYSTEM_RAM);
      info.info      = glsl_shader->variable;
      info.info_elem = glsl_shader->variables;

#ifdef HAVE_PYTHON
      info.script = glsl_shader->script;
      info.script_class = *glsl_shader->script_class ?
         glsl_shader->script_class : NULL;
#endif

      gl_state_tracker = state_tracker_init(&info);
      if (!gl_state_tracker)
         RARCH_WARN("Failed to init state tracker.\n");
   }
   
   glsl_enable = true;
   gl_program[glsl_shader->passes  + 1] = gl_program[0];
   gl_uniforms[glsl_shader->passes + 1] = gl_uniforms[0];

   if (glsl_shader->modern)
   {
      gl_program[GL_SHADER_STOCK_BLEND] = compile_program(glsl_core ? stock_vertex_core_blend : stock_vertex_modern_blend,
            glsl_core ? stock_fragment_core_blend : stock_fragment_modern_blend, GL_SHADER_STOCK_BLEND);
      find_uniforms(0, gl_program[GL_SHADER_STOCK_BLEND], &gl_uniforms[GL_SHADER_STOCK_BLEND]);
   }
   else
   {
      gl_program[GL_SHADER_STOCK_BLEND] = gl_program[0];
      gl_uniforms[GL_SHADER_STOCK_BLEND] = gl_uniforms[0];
   }

   gl_glsl_reset_attrib();

   for (i = 0; i < GFX_MAX_SHADERS; i++)
   {
      glGenBuffers(1, &glsl_vbo[i].vbo_primary);
      glGenBuffers(1, &glsl_vbo[i].vbo_secondary);
   }

   return true;

error:
   gl_glsl_deinit();
   return false;
}
Example #12
0
int main( int argc, char *argv[] )
{
    time_t curtime;
    struct tm *loctime;
    int value, code;
    char * d, *d1;

    char * sourcefile = 0;
    char basepathname[__MAX_PATH] = "";
    char dcbname[__MAX_PATH] = "";
    char stubname[__MAX_PATH] = "";
    char importname[__MAX_PATH] = "";
    char compilerimport[__MAX_PATH] = "";
    int i, j;
    char *ptr;

    /* get my executable name */
    ptr = argv[0] + strlen( argv[0] );
    while ( ptr > argv[0] && ptr[-1] != '\\' && ptr[-1] != '/' ) ptr-- ;
    appexename = strdup( ptr );

    /* get executable full pathname  */
    appexefullpath = getfullpath( argv[0] );
    if ( ( !strchr( argv[0], '\\' ) && !strchr( argv[0], '/' ) ) && !file_exists( appexefullpath ) )
    {
        char *p = whereis( appexename );
        if ( p )
        {
            char * tmp = calloc( 1, strlen( p ) + strlen( appexename ) + 2 );
            free( appexefullpath );
            sprintf( tmp, "%s/%s", p, appexename );
            appexefullpath = getfullpath( tmp );
            free( tmp );
        }
    }

    /* get pathname of executable */
    ptr = strstr( appexefullpath, appexename );
    appexepath = calloc( 1, ptr - appexefullpath + 1 );
    strncpy( appexepath, appexefullpath, ptr - appexefullpath );

    printf( BGDC_VERSION "\n"
            "Bennu Game Development Compiler\n"
            "\n"
            "Copyright (c) 2006-2016 SplinterGU (Fenix/BennuGD)\n"
            "Copyright (c) 2002-2006 Fenix Team (Fenix)\n"
            "Copyright (c) 1999-2002 José Luis Cebrián Pagüe (Fenix)\n"
            "\n" );

    /* Default lang to EN */
    strcpy( langinfo, "EN" );
    /* LANG detect */
#ifdef WIN32
    GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, langinfo, 64 );
    strlwr( langinfo );
#else
    if ( getenv( "LANG" ) != NULL && strlen( getenv( "LANG" ) ) >= 2 )
        strcpy( langinfo, getenv( "LANG" ) );
#endif
    langinfo[2] = 0;

    srand( time( NULL ) );

    /* build error messages list */
    err_buildErrorTable();

    init_c_type();
    identifier_init();
    constants_init();
    string_init();
    compile_init();

    mainproc = procdef_new( procdef_getid(), identifier_search_or_add( "MAIN" ) ) ;

    /* Init vars */

    char tmp_version[ 32 ];
    sprintf( tmp_version, "\"%s\"", VERSION );
    add_simple_define( "COMPILER_VERSION", tmp_version );
    add_simple_define( "__VERSION__", tmp_version );

    curtime = time( NULL ); /* Get the current time. */
    loctime = localtime( &curtime ); /* Convert it to local time representation. */

    strftime( timebuff, sizeof( timebuff ), "%Y/%m/%d", loctime );
    value = string_new( timebuff );
    code = identifier_search_or_add( "__DATE__" ) ;
    constants_add( code, typedef_new( TYPE_STRING ), value ) ;

    strftime( timebuff, sizeof( timebuff ), "%H:%M:%S", loctime );
    value = string_new( timebuff );
    code = identifier_search_or_add( "__TIME__" ) ;
    constants_add( code, typedef_new( TYPE_STRING ), value ) ;
/*
    value = string_new( VERSION );
    code = identifier_search_or_add( "__VERSION__" ) ;
    constants_add( code, typedef_new( TYPE_STRING ), value ) ;
    code = identifier_search_or_add( "COMPILER_VERSION" ) ;
    constants_add( code, typedef_new( TYPE_STRING ), value ) ;
*/
    strcpy( _tmp, VERSION );
                d = strchr( _tmp, '.' ); *d = '\0'; add_simple_define( "__BGD__", _tmp );
    d1 = d + 1; d = strchr(   d1, '.' ); *d = '\0'; add_simple_define( "__BGD_MINOR__", d1 );
    d1 = d + 1;                                     add_simple_define( "__BGD_PATCHLEVEL__", d1 );

    memset( &dcb, 0, sizeof( dcb ) );

    core_init();
    sysproc_init();

    /* Get command line parameters */

    for ( i = 1 ; i < argc ; i++ )
    {
        if ( argv[i][0] == '-' )
        {
            if ( !strcmp( argv[i], "--pedantic" ) )
            {
                autodeclare = 0 ;
                continue;
            }

            if ( !strcmp( argv[i], "--libmode" ) )
            {
                libmode = 1 ;
                continue;
            }

            j = 1;
            while ( argv[i][j] )
            {
                if ( argv[i][j] == 'd' )
                {
                    if ( argv[i][j + 1] >= '0' && argv[i][j + 1] <= '9' )
                    {
                        debug = atoi( &argv[i][j + 1] );
                    }
                    else
                    {
                        debug = 1;
                    }
                }

                if ( argv[i][j] == 'o' )
                {
                    if ( argv[i][j + 1] )
                        strncpy( dcbname, &argv[i][j + 1], sizeof( dcbname ) );
                    else if ( argv[i + 1] && argv[i + 1][0] != '-' )
                        strncpy( dcbname, argv[++i], sizeof( dcbname ) );
                    break;
                }

                if ( argv[i][j] == 'c' ) dos_chars = 1;

                if ( argv[i][j] == 'a' ) autoinclude = 1;

                if ( argv[i][j] == 'g' ) dcb_options |= DCB_DEBUG;

                if ( argv[i][j] == 'p' ) autodeclare = 0 ;

                if ( argv[i][j] == 's' )
                {
                    /* -s "stub": Use a stub */

                    if ( argv[i][j + 1] )
                        strncpy( stubname, &argv[i][j + 1], __MAX_PATH );
                    else if ( argv[i + 1] && argv[i + 1][0] != '-' )
                        strncpy( stubname, argv[++i], __MAX_PATH );
                    break;
                }

                if ( argv[i][j] == 'f' )
                {
                    /* -f "file": Embed a file to the DCB */

                    if ( argv[i][j + 1] )
                        dcb_add_file( &argv[i][j + 1] );
                    else while ( argv[i + 1] )
                        {
                            if ( argv[i + 1][0] == '-' ) break;
                            dcb_add_file( argv[i + 1] );
                            i++;
                        }
                    break;
                }

                if ( argv[i][j] == 'i' )
                {
                    /* -i "path": add a file to the path for include files */

                    if ( argv[i][j + 1] == 0 )
                    {
                        if ( i == argc - 1 )
                        {
                            printf( MSG_DIRECTORY_MISSING "\n" );
                            exit( 1 );
                        }
                        file_addp( argv[i + 1] );
                        i++;
                        break;
                    }
                    file_addp( &argv[i][j + 1] );
                    break;
                }

                if ( argv[i][j] == 'l' )
                {
                    /* -lLANG:  Set the language for errors and messages */

                    if ( argv[i][j + 1] == 0 )
                    {
                        if ( i != argc - 1 )
                        {
                            strcpy( langinfo, argv[i + 1] );
                        }
                        i++;
                        break;
                    }
                    strcpy( langinfo, &argv[i][j + 1] );
                    break;
                }

                if ( argv[i][j] == 'D' )
                {
                    char * macro = NULL ;
                    char * text = NULL ;

                    /* -D<macro>=<text> */

                    if ( argv[i][j + 1] )
                    {
                        macro = strdup( &argv[i][j + 1] );
                    }
                    else
                    {
                        if ( argv[i + 1][0] == '-' ) break;
                        macro = strdup( argv[i + 1] );
                        i++;
                    }

                    if (( text = strchr( macro, '=' ) ) )
                    {
                        * text = '\0';
                        text++;
                    }
                    else
                    {
                        text = "";
                    }

                    add_simple_define( macro, text );
                    free( macro );
                    break;
                }

                if ( argv[i][j] == 'C' )
                {
                    if ( argv[i][j + 1] == 'a' ) autodeclare = 1 ;
                    break;
                }

                if ( argv[i][j] == 'L' )
                {
                    int r = 1;
                    char * f;
                    if ( argv[i][j + 1] )
                        r = dcb_load_lib( ( f = &argv[i][j + 1] ) );
                    else if ( argv[i + 1] && argv[i + 1][0] != '-' )
                    {
                        r = dcb_load_lib( ( f = argv[i + 1] ) );
                        i++;
                    }

                    switch ( r )
                    {
                        case    0:
                                printf( "ERROR: %s doesn't exist or isn't version DCB compatible\n", f ) ;
                                exit( -1 );

                        case    -1:
                                printf( "ERROR: %s isn't 7.10 DCB version, you need a 7.10 version or greater for use this feature\n", f ) ;
                                exit( -1 );
                    }
                    break;
                }

                j++;
            }
        }
        else
        {
/*
            if ( sourcefile )
            {
                printf( MSG_TOO_MANY_FILES "\n" );
                return 0;
            }
*/
            char * p, * pathend = NULL;

            sourcefile = argv[i];
            p = main_path = strdup( argv[i] );
            while ( p && *p )
            {
                if ( *p == ':' || *p == '\\' || *p == '/' ) pathend = p;
                p++;
            }
            if ( pathend )
            {
                *( pathend + 1 ) = '\0';
                file_addp( main_path );
            }
            else
            {
                free( main_path );
                main_path = getcwd(malloc(__MAX_PATH), __MAX_PATH);
                strcat(main_path, PATH_SEP);
            }

            /* Files names */

            strcpy( basepathname, sourcefile );
            REMOVE_EXT( basepathname );

            /* Default compiler imports */
            strcpy( compilerimport, argv[0] );
#ifdef WIN32
            REMOVE_EXT( compilerimport );
#endif
            strcat( compilerimport, ".imp" );
            import_files( compilerimport );
            strcat( compilerimport, "ort" ); /* name.import */
            import_files( compilerimport );

            /* Project imports */
            strcpy( importname, basepathname ); strcat( importname, ".imp" );
            import_files( importname );

            strcat( importname, "ort" ); /* name.import */
            import_files( importname );

            /* Load Main Source File */
            load_file( sourcefile );

            if ( !dcbname[0] )
            {
                strcpy( dcbname, basepathname ); strcat( dcbname, !libmode ? ".dcb" : ".dcl" );
            }
        }
    }

    if ( !sourcefile )
    {
        printf( MSG_USING
                MSG_OPTION_D
                MSG_OPTIONS
                MSG_LICENSE, argv[0] );
        return 0;
    }

    compile_program();

    if ( stubname[0] != 0 )
    {
        if ( !file_exists( stubname ) )
        {
#ifdef WIN32
            char exepath[__MAX_PATH];

            GetModuleFileName( NULL, exepath, sizeof( exepath ) );
            PathRemoveFileSpec( exepath );
            strcat( exepath, "\\" );
            memmove( stubname + strlen( exepath ), stubname, strlen( stubname ) + 1 );
            memcpy( stubname, exepath, strlen( exepath ) );
#else
            const char * ptr = argv[0] + strlen( argv[0] );
            while ( ptr > argv[0] && *ptr != '\\' && *ptr != '/' ) ptr--;
            if ( *ptr == '\\' || *ptr == '/' ) ptr++;
            if ( ptr > argv[0] )
            {
                memmove( stubname + ( ptr - argv[0] ), stubname, strlen( stubname ) + 1 );
                memcpy( stubname, argv[0], ptr - argv[0] );
            }
#endif
            if ( !file_exists( stubname ) )
            {
#ifdef WIN32
                strcat( stubname, ".exe" );
                if ( !file_exists( stubname ) )
                {
#endif
                    compile_error( "Can't open stub file %s", stubname );
#ifdef WIN32
                    return -1;
                }
#endif

            }
        }

        REMOVE_EXT( dcbname );
#ifdef WIN32
        strcat( dcbname, ".exe" );
#endif
        dcb_save( dcbname, dcb_options, stubname );
    }
    else
    {
        dcb_save( dcbname, dcb_options, NULL );
    }

    /* destroy error messages list */
    err_destroyErrorTable();

    return 1;
}
Example #13
0
static bool gl_glsl_init(void *data, const char *path)
{
    unsigned i;
    config_file_t *conf        = NULL;
    glsl_shader_data_t *glsl   = NULL;
    const char *stock_vertex   = NULL;
    const char *stock_fragment = NULL;
    driver_t *driver           = driver_get_ptr();

    (void)data;

    glsl = (glsl_shader_data_t*)calloc(1, sizeof(glsl_shader_data_t));

    if (!glsl)
        return false;

#ifndef HAVE_OPENGLES2
    RARCH_LOG("Checking GLSL shader support ...\n");
    bool shader_support = glCreateProgram && glUseProgram && glCreateShader
                          && glDeleteShader && glShaderSource && glCompileShader && glAttachShader
                          && glDetachShader && glLinkProgram && glGetUniformLocation
                          && glUniform1i && glUniform1f && glUniform2fv && glUniform4fv
                          && glUniformMatrix4fv
                          && glGetShaderiv && glGetShaderInfoLog && glGetProgramiv
                          && glGetProgramInfoLog
                          && glDeleteProgram && glGetAttachedShaders
                          && glGetAttribLocation && glEnableVertexAttribArray
                          && glDisableVertexAttribArray
                          && glVertexAttribPointer
                          && glGenBuffers && glBufferData && glDeleteBuffers && glBindBuffer;

    if (!shader_support)
    {
        RARCH_ERR("GLSL shaders aren't supported by your OpenGL driver.\n");
        free(glsl);
        return false;
    }
#endif

    glsl->shader = (struct video_shader*)calloc(1, sizeof(*glsl->shader));
    if (!glsl->shader)
    {
        free(glsl);
        return false;
    }

    if (path)
    {
        bool ret;
        const char *path_ext = path_get_extension(path);

        if (!strcmp(path_ext, "glsl"))
        {
            strlcpy(glsl->shader->pass[0].source.path, path,
                    sizeof(glsl->shader->pass[0].source.path));
            glsl->shader->passes = 1;
            glsl->shader->modern = true;
            ret = true;
        }
        else if (!strcmp(path_ext, "glslp"))
        {
            conf = config_file_new(path);
            if (conf)
            {
                ret = video_shader_read_conf_cgp(conf, glsl->shader);
                glsl->shader->modern = true;
            }
            else
                ret = false;
        }
        else
            ret = false;

        if (!ret)
        {
            RARCH_ERR("[GL]: Failed to parse GLSL shader.\n");
            free(glsl->shader);
            free(glsl);
            return false;
        }
    }
    else
    {
        RARCH_WARN("[GL]: Stock GLSL shaders will be used.\n");
        glsl->shader->passes = 1;
        glsl->shader->pass[0].source.string.vertex   =
            strdup(glsl_core ? stock_vertex_core : stock_vertex_modern);
        glsl->shader->pass[0].source.string.fragment =
            strdup(glsl_core ? stock_fragment_core : stock_fragment_modern);
        glsl->shader->modern = true;
    }

    video_shader_resolve_relative(glsl->shader, path);
    video_shader_resolve_parameters(conf, glsl->shader);

    if (conf)
    {
        config_file_free(conf);
        conf = NULL;
    }

    stock_vertex = (glsl->shader->modern) ?
                   stock_vertex_modern : stock_vertex_legacy;
    stock_fragment = (glsl->shader->modern) ?
                     stock_fragment_modern : stock_fragment_legacy;

    if (glsl_core)
    {
        stock_vertex = stock_vertex_core;
        stock_fragment = stock_fragment_core;
    }

#ifdef HAVE_OPENGLES2
    if (!glsl->shader->modern)
    {
        RARCH_ERR("[GL]: GLES context is used, but shader is not modern. Cannot use it.\n");
        goto error;
    }
#else
    if (glsl_core && !glsl->shader->modern)
    {
        RARCH_ERR("[GL]: GL core context is used, but shader is not core compatible. Cannot use it.\n");
        goto error;
    }
#endif

    /* Find all aliases we use in our GLSLP and add #defines for them so
     * that a shader can choose a fallback if we are not using a preset. */
    *glsl->glsl_alias_define = '\0';
    for (i = 0; i < glsl->shader->passes; i++)
    {
        if (*glsl->shader->pass[i].alias)
        {
            char define[128] = {0};

            snprintf(define, sizeof(define), "#define %s_ALIAS\n",
                     glsl->shader->pass[i].alias);
            strlcat(glsl->glsl_alias_define, define, sizeof(glsl->glsl_alias_define));
        }
    }

    if (!(glsl->gl_program[0] = compile_program(glsl, stock_vertex, stock_fragment, 0)))
    {
        RARCH_ERR("GLSL stock programs failed to compile.\n");
        goto error;
    }

    if (!compile_programs(glsl, &glsl->gl_program[1]))
        goto error;

    if (!gl_load_luts(glsl->shader, glsl->gl_teximage))
    {
        RARCH_ERR("[GL]: Failed to load LUTs.\n");
        goto error;
    }

    for (i = 0; i <= glsl->shader->passes; i++)
        find_uniforms(glsl, i, glsl->gl_program[i], &glsl->gl_uniforms[i]);

#ifdef GLSL_DEBUG
    if (!gl_check_error())
        RARCH_WARN("Detected GL error in GLSL.\n");
#endif

    if (glsl->shader->variables)
    {
        struct state_tracker_info info = {0};

        info.wram      = (uint8_t*)pretro_get_memory_data(RETRO_MEMORY_SYSTEM_RAM);
        info.info      = glsl->shader->variable;
        info.info_elem = glsl->shader->variables;

#ifdef HAVE_PYTHON
        info.script = glsl->shader->script;
        info.script_class = *glsl->shader->script_class ?
                            glsl->shader->script_class : NULL;
#endif

        glsl->gl_state_tracker = state_tracker_init(&info);
        if (!glsl->gl_state_tracker)
            RARCH_WARN("Failed to init state tracker.\n");
    }

    glsl->gl_program[glsl->shader->passes  + 1] = glsl->gl_program[0];
    glsl->gl_uniforms[glsl->shader->passes + 1] = glsl->gl_uniforms[0];

    if (glsl->shader->modern)
    {
        glsl->gl_program[GL_SHADER_STOCK_BLEND] = compile_program(
                    glsl,
                    glsl_core ?
                    stock_vertex_core_blend : stock_vertex_modern_blend,
                    glsl_core ?
                    stock_fragment_core_blend : stock_fragment_modern_blend,
                    GL_SHADER_STOCK_BLEND);
        find_uniforms(glsl, 0, glsl->gl_program[GL_SHADER_STOCK_BLEND],
                      &glsl->gl_uniforms[GL_SHADER_STOCK_BLEND]);
    }
    else
    {
        glsl->gl_program [GL_SHADER_STOCK_BLEND] = glsl->gl_program[0];
        glsl->gl_uniforms[GL_SHADER_STOCK_BLEND] = glsl->gl_uniforms[0];
    }

    gl_glsl_reset_attrib(glsl);

    for (i = 0; i < GFX_MAX_SHADERS; i++)
    {
        glGenBuffers(1, &glsl->glsl_vbo[i].vbo_primary);
        glGenBuffers(1, &glsl->glsl_vbo[i].vbo_secondary);
    }

    driver->video_shader_data = glsl;

    return true;

error:
    gl_glsl_destroy_resources(glsl);

    if (glsl)
        free(glsl);

    return false;
}
Example #14
0
Program compile_ff_program(int flags)
{
	const auto shader_input = create_ff(flags);
	return compile_program(shader_input.vs, shader_input.fs, shader_input.debug_name);
}
Example #15
0
enum piglit_result
piglit_cl_test(const int argc,
               const char** argv,
               const struct piglit_cl_api_test_config* config,
               const struct piglit_cl_api_test_env* env)
{
#if defined(CL_VERSION_1_2)
	enum piglit_result result = PIGLIT_PASS;

	int i;
	cl_program_binary_type* binary_type;
	cl_program compiled_programs[2];
	cl_program function_prog;
	cl_program kernel_prog;
	cl_program linked_prog;

	/* Create compiled program */
	function_prog = compile_program(env->context->cl_ctx,
	                                env->context->num_devices, env->context->device_ids,
	                                1, &strings[0],
	                                "function program");
	kernel_prog = compile_program(env->context->cl_ctx,
	                              env->context->num_devices, env->context->device_ids,
	                              2, &strings[1],
                                 "kernel program");

	if (!function_prog || !kernel_prog) {
		clReleaseProgram(function_prog);
		clReleaseProgram(kernel_prog);
		return PIGLIT_FAIL;
	}

	compiled_programs[0] = function_prog;
	compiled_programs[1] = kernel_prog;

/*** Normal usage ***/
	test(env->context->cl_ctx,
	     env->context->num_devices, env->context->device_ids,
	     "-create-library",
	     1, compiled_programs,
	     NULL, NULL,
	     &linked_prog,
	     CL_SUCCESS, &result, "Link program as library");

	for(i = 0; i < env->context->num_devices; ++i) {
		binary_type = piglit_cl_get_program_build_info(linked_prog,
		                                               env->context->device_ids[i],
		                                               CL_PROGRAM_BINARY_TYPE);
		if (*binary_type != CL_PROGRAM_BINARY_TYPE_LIBRARY) {
			piglit_merge_result(&result, PIGLIT_FAIL);
			fprintf(stderr,
		           "Failed: binary is not of type CL_PROGRAM_BINARY_TYPE_LIBRARY.\n");
		}
		free(binary_type);
	}

	clReleaseProgram(linked_prog);

	test(env->context->cl_ctx,
	     env->context->num_devices, env->context->device_ids,
	     "",
	     2, compiled_programs,
	     NULL, NULL,
	     &linked_prog,
	     CL_SUCCESS, &result, "Link program as executable");

	for(i = 0; i < env->context->num_devices; ++i) {
		binary_type = piglit_cl_get_program_build_info(linked_prog,
		                                               env->context->device_ids[i],
		                                               CL_PROGRAM_BINARY_TYPE);
		if (*binary_type != CL_PROGRAM_BINARY_TYPE_EXECUTABLE) {
			piglit_merge_result(&result, PIGLIT_FAIL);
			fprintf(stderr,
		           "Failed: binary is not of type CL_PROGRAM_BINARY_TYPE_EXECUTABLE.\n");
		}
		free(binary_type);
	}


/*** Errors ***/

	/*
	 * CL_INVALID_VALUE if device_list is NULL and num_devices is greater than
	 * zero, or if device_list is not NULL and num_devices is zero
	 */
	test(env->context->cl_ctx,
	     env->context->num_devices, NULL,
	     "",
	     2, compiled_programs,
	     NULL, NULL,
	     NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE if device_list is NULL and num_devices is greater than zero");

	test(env->context->cl_ctx,
	     0, env->context->device_ids,
	     "",
	     2, compiled_programs,
	     NULL, NULL,
	     NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE if device_list is not NULL and num_devices is zero");

	/*
	 * CL_INVALID_VALUE if num_input_programs is zero and input_programs is NULL
	 * or if num_input_programs is zero and input_programs is not NULL
	 * or if num_input_programs is not zero and input_programs is NULL
	 */
	test(env->context->cl_ctx,
	     env->context->num_devices, env->context->device_ids,
	     "",
	     0, NULL,
	     NULL, NULL,
	     NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE if num_input_programs is zero and input_programs is NULL");

	test(env->context->cl_ctx,
	     env->context->num_devices, env->context->device_ids,
	     "",
	     0, compiled_programs,
	     NULL, NULL,
	     NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE if num_input_programs is zero and input_programs is not NULL");

	test(env->context->cl_ctx,
	     env->context->num_devices, env->context->device_ids,
	     "",
	     2, NULL,
	     NULL, NULL,
	     NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE if num_input_programs is not zero and input_programs is NULL");

	/*
	 * CL_INVALID_PROGRAM if programs specified in input_programs are not valid program objects
	 */


	/*
	 * CL_INVALID_VALUE if pfn_notify is NULL but user_data is not NULL.
	 */
	test(env->context->cl_ctx,
	     env->context->num_devices, env->context->device_ids,
	     "",
	     2, compiled_programs,
	     NULL, &i,
	     NULL,
	     CL_INVALID_VALUE, &result,
	     "Trigger CL_INVALID_VALUE if pfn_notify is NULL but user_data is not NULL");

	/*
	 * CL_INVALID_DEVICE if OpenCL devices listed in device_list are not in the
	 * list of devices associated with context
	 */


	/*
	 * CL_INVALID_LINKER_OPTIONS if the linker options specified by options are
	 * invalid
	 */
	test(env->context->cl_ctx,
	     env->context->num_devices, env->context->device_ids,
	     "-invalid- --link-- options",
	     2, compiled_programs,
	     NULL, NULL,
	     NULL,
	     CL_INVALID_LINKER_OPTIONS, &result,
	     "Trigger CL_INVALID_LINKER_OPTIONS if the linker options specified by options are invalid");

	/*
	 * CL_INVALID_OPERATION if the compilation or build of a program executable
	 * for any of the devices listed in device_list by a previous call to
	 * clCompileProgram or clBuildProgram for program has not completed
	 */


	/*
	 * CL_INVALID_OPERATION if the rules for devices containing compiled binaries
	 * or libraries as described in input_programs argument above are not followed
	 */
	compiled_programs[0] = linked_prog;
	test(env->context->cl_ctx,
	     env->context->num_devices, env->context->device_ids,
	     "",
	     2, compiled_programs,
	     NULL, NULL,
	     NULL,
	     CL_INVALID_OPERATION, &result,
	     "Trigger CL_INVALID_OPERATION if the rules for devices containing compiled binaries or libraries as described in input_programs argument above are not followed");

	/*
	 * CL_LINKER_NOT_AVAILABLE if a linker is not available
	 * i.e. CL_DEVICE_LINKER_AVAILABLE specified in the table of allowed values
	 * for param_name for clGetDeviceInfo is set to CL_FALSE.
	 */
	for(i = 0; i < env->context->num_devices; ++i) {
		cl_bool* linker_available =
			piglit_cl_get_device_info(env->context->device_ids[i],
			                          CL_DEVICE_LINKER_AVAILABLE);
			if(!(*linker_available)) {
				test(env->context->cl_ctx,
				     env->context->num_devices, env->context->device_ids,
				     "",
				     2, compiled_programs,
				     NULL, NULL,
				     NULL,
				     CL_LINKER_NOT_AVAILABLE, &result,
				     "Trigger CL_LINKER_NOT_AVAILABLE if a linker is not available");
		}
		free(linker_available);
	}


/* Release programs */
	clReleaseProgram(function_prog);
	clReleaseProgram(kernel_prog);
	clReleaseProgram(linked_prog);

	/*
	 * CL_LINK_PROGRAM_FAILURE if there is a failure to link the compiled binaries
	 * and/or libraries.
	 */
	function_prog = compile_program(env->context->cl_ctx,
	                                env->context->num_devices, env->context->device_ids,
	                                1, &strings[0],
	                                "2nd function program");
	kernel_prog = compile_program(env->context->cl_ctx,
	                              env->context->num_devices, env->context->device_ids,
	                              2, &strings[2],
                                 "2nd kernel program");

	if (!function_prog || !kernel_prog) {
		result = PIGLIT_FAIL;
	} else {
		compiled_programs[0] = function_prog;
		compiled_programs[1] = kernel_prog;

		test(env->context->cl_ctx,
		     env->context->num_devices, env->context->device_ids,
		     "",
		     2, compiled_programs,
		     NULL, NULL,
		     NULL,
		     CL_LINK_PROGRAM_FAILURE, &result,
		     "Trigger CL_LINK_PROGRAM_FAILURE if there is a failure to link the compiled binaries and/or libraries");
	}

/* Release programs */
	clReleaseProgram(function_prog);
	clReleaseProgram(kernel_prog);

	return result;
#else
	return PIGLIT_SKIP;
#endif
}
Example #16
0
int
main (int argc, char **argv)
{
    GTimer *timer;
    int frame_count;
    GLuint handle;
    GLenum err;
    GTimer *frame_timer;
    double direction = OPACITY_INCREMENT;

    g_type_init();

    grand = g_rand_new();

    frame_timer = g_timer_new();
    g_timer_start(frame_timer);

    load_data();
    canvas_pixbuf = gdk_pixbuf_new_from_file(CANVAS_FILENAME, NULL);
    g_assert(canvas_pixbuf);

    glutInit(&argc, argv);
    glutCreateWindow("color-convert");
    glutFullScreen();

    err = glewInit();
    g_assert(err == GLEW_OK);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, (float) WIDTH, (float) HEIGHT, 0.0f, -1.0f, 1.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glDepthRange(0.0f, 1.0f);
    glViewport(0, 0, WIDTH, HEIGHT);
    glRasterPos2f(0.0f, 0.0f);

    glEnable(GL_TEXTURE_2D);

    handle = compile_program();
    glUseProgram(handle);
    setup_textures(handle);

    frame_count = 1;
    timer = g_timer_new();

    while (TRUE) {
        double elapsed;
        GLint location;

        opacity += direction;
        if (opacity >= 1.0) {
            direction = -OPACITY_INCREMENT;
        } else if (opacity <= 0.0) {
            direction = OPACITY_INCREMENT;
        }

        location = glGetUniformLocation(handle, "opacity");
        glUniform1f(location, opacity);

        upload_data();
        draw_scene();

        glutSwapBuffers();

        frame_count ++;

        elapsed = g_timer_elapsed(frame_timer, NULL) * 1000;
        if (elapsed < FRAME_TIME_MS) {
            g_usleep(1000 * (FRAME_TIME_MS - elapsed));
        }

        g_timer_start(frame_timer);

        if (frame_count % 1000 == 0) {
            double fps;

            fps = 1000 / g_timer_elapsed(timer, NULL);
            g_timer_start(timer);
            g_print("Framerate: %gfps\n", fps);
        }
    }
}
Example #17
0
static bool gl_glsl_init(const char *path)
{
#if !defined(HAVE_OPENGLES2) && !defined(HAVE_OPENGL_MODERN) && !defined(__APPLE__)
   // Load shader functions.
   LOAD_GL_SYM(CreateProgram);
   LOAD_GL_SYM(UseProgram);
   LOAD_GL_SYM(CreateShader);
   LOAD_GL_SYM(DeleteShader);
   LOAD_GL_SYM(ShaderSource);
   LOAD_GL_SYM(CompileShader);
   LOAD_GL_SYM(AttachShader);
   LOAD_GL_SYM(DetachShader);
   LOAD_GL_SYM(LinkProgram);
   LOAD_GL_SYM(GetUniformLocation);
   LOAD_GL_SYM(Uniform1i);
   LOAD_GL_SYM(Uniform1f);
   LOAD_GL_SYM(Uniform2fv);
   LOAD_GL_SYM(Uniform4fv);
   LOAD_GL_SYM(UniformMatrix4fv);
   LOAD_GL_SYM(GetShaderiv);
   LOAD_GL_SYM(GetShaderInfoLog);
   LOAD_GL_SYM(GetProgramiv);
   LOAD_GL_SYM(GetProgramInfoLog);
   LOAD_GL_SYM(DeleteProgram);
   LOAD_GL_SYM(GetAttachedShaders);
   LOAD_GL_SYM(GetAttribLocation);
   LOAD_GL_SYM(EnableVertexAttribArray);
   LOAD_GL_SYM(DisableVertexAttribArray);
   LOAD_GL_SYM(VertexAttribPointer);

   RARCH_LOG("Checking GLSL shader support ...\n");
   bool shader_support = pglCreateProgram && pglUseProgram && pglCreateShader
      && pglDeleteShader && pglShaderSource && pglCompileShader && pglAttachShader
      && pglDetachShader && pglLinkProgram && pglGetUniformLocation
      && pglUniform1i && pglUniform1f && pglUniform2fv && pglUniform4fv && pglUniformMatrix4fv
      && pglGetShaderiv && pglGetShaderInfoLog && pglGetProgramiv && pglGetProgramInfoLog 
      && pglDeleteProgram && pglGetAttachedShaders
      && pglGetAttribLocation && pglEnableVertexAttribArray && pglDisableVertexAttribArray
      && pglVertexAttribPointer;

   if (!shader_support)
   {
      RARCH_ERR("GLSL shaders aren't supported by your OpenGL driver.\n");
      return false;
   }
#endif

   glsl_shader = (struct gfx_shader*)calloc(1, sizeof(*glsl_shader));
   if (!glsl_shader)
      return false;

   if (path)
   {
      bool ret;
      if (strcmp(path_get_extension(path), "glsl") == 0)
      {
         strlcpy(glsl_shader->pass[0].source.cg, path, sizeof(glsl_shader->pass[0].source.cg));
         glsl_shader->passes = 1;
         glsl_shader->modern = true;
         ret = true;
      }
      else if (strcmp(path_get_extension(path), "glslp") == 0)
      {
         config_file_t *conf = config_file_new(path);
         if (conf)
         {
            ret = gfx_shader_read_conf_cgp(conf, glsl_shader);
            glsl_shader->modern = true;
            config_file_free(conf);
         }
         else
            ret = false;
      }
      else
         ret = gfx_shader_read_xml(path, glsl_shader);

      if (!ret)
      {
         RARCH_ERR("[GL]: Failed to parse GLSL shader.\n");
         return false;
      }
   }
   else
   {
      RARCH_WARN("[GL]: Stock GLSL shaders will be used.\n");
      glsl_shader->passes = 1;
      glsl_shader->pass[0].source.xml.vertex   = strdup(stock_vertex_modern);
      glsl_shader->pass[0].source.xml.fragment = strdup(stock_fragment_modern);
      glsl_shader->modern = true;
   }

   gfx_shader_resolve_relative(glsl_shader, path);

#ifdef HAVE_OPENGLES2
   if (!glsl_shader->modern)
   {
      RARCH_ERR("[GL]: GLES context is used, but shader is not modern. Cannot use it.\n");
      return false;
   }
#endif

   const char *stock_vertex = glsl_shader->modern ?
      stock_vertex_modern : stock_vertex_legacy;
   const char *stock_fragment = glsl_shader->modern ?
      stock_fragment_modern : stock_fragment_legacy;

   if (!(gl_program[0] = compile_program(stock_vertex, stock_fragment, 0)))
   {
      RARCH_ERR("GLSL stock programs failed to compile.\n");
      gl_glsl_free_shader();
      return false;
   }

   if (!compile_programs(&gl_program[1]))
   {
      gl_glsl_free_shader();
      return false;
   }

   if (!load_luts())
   {
      RARCH_ERR("[GL]: Failed to load LUTs.\n");
      gl_glsl_free_shader();
      return false;
   }

   for (unsigned i = 0; i <= glsl_shader->passes; i++)
      find_uniforms(gl_program[i], &gl_uniforms[i]);

#ifdef GLSL_DEBUG
   if (!gl_check_error())
      RARCH_WARN("Detected GL error in GLSL.\n");
#endif

   if (glsl_shader->variables)
   {
      struct state_tracker_info info = {0};
      info.wram      = (uint8_t*)pretro_get_memory_data(RETRO_MEMORY_SYSTEM_RAM);
      info.info      = glsl_shader->variable;
      info.info_elem = glsl_shader->variables;

#ifdef HAVE_PYTHON
      info.script = glsl_shader->script;
      info.script_class = *glsl_shader->script_class ?
         glsl_shader->script_class : NULL;
#endif

      gl_state_tracker = state_tracker_init(&info);
      if (!gl_state_tracker)
         RARCH_WARN("Failed to init state tracker.\n");
   }
   
   glsl_enable = true;
   gl_program[glsl_shader->passes  + 1] = gl_program[0];
   gl_uniforms[glsl_shader->passes + 1] = gl_uniforms[0];

   if (glsl_shader->modern)
   {
      gl_program[GL_SHADER_STOCK_BLEND] = compile_program(stock_vertex_modern_blend,
            stock_fragment_modern_blend, GL_SHADER_STOCK_BLEND);

      find_uniforms(gl_program[GL_SHADER_STOCK_BLEND], &gl_uniforms[GL_SHADER_STOCK_BLEND]);
   }
   else
   {
      gl_program[GL_SHADER_STOCK_BLEND] = gl_program[0];
      gl_uniforms[GL_SHADER_STOCK_BLEND] = gl_uniforms[0];
   }

   gl_glsl_reset_attrib();

   return true;
}