Example #1
0
bool RenderChain::compile_shaders(CGprogram &fPrg, CGprogram &vPrg, const std::string &shader)
{
#ifdef HAVE_CG
   CGprofile vertex_profile = cgD3D9GetLatestVertexProfile();
   CGprofile fragment_profile = cgD3D9GetLatestPixelProfile();
   RARCH_LOG("[D3D Cg]: Vertex profile: %s\n", cgGetProfileString(vertex_profile));
   RARCH_LOG("[D3D Cg]: Fragment profile: %s\n", cgGetProfileString(fragment_profile));
   const char **fragment_opts = cgD3D9GetOptimalOptions(fragment_profile);
   const char **vertex_opts = cgD3D9GetOptimalOptions(vertex_profile);

   if (shader.length() > 0)
   {
      RARCH_LOG("[D3D Cg]: Compiling shader: %s.\n", shader.c_str());
      fPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE,
            shader.c_str(), fragment_profile, "main_fragment", fragment_opts);

      if (cgGetLastListing(cgCtx))
         RARCH_ERR("[D3D Cg]: Fragment error:\n%s\n", cgGetLastListing(cgCtx));

      vPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE,
            shader.c_str(), vertex_profile, "main_vertex", vertex_opts);

      if (cgGetLastListing(cgCtx))
         RARCH_ERR("[D3D Cg]: Vertex error:\n%s\n", cgGetLastListing(cgCtx));
   }
   else
   {
      RARCH_LOG("[D3D Cg]: Compiling stock shader.\n");

      fPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_program,
            fragment_profile, "main_fragment", fragment_opts);

      if (cgGetLastListing(cgCtx))
         RARCH_ERR("[D3D Cg]: Fragment error:\n%s\n", cgGetLastListing(cgCtx));

      vPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_program,
            vertex_profile, "main_vertex", vertex_opts);

      if (cgGetLastListing(cgCtx))
         RARCH_ERR("[D3D Cg]: Vertex error:\n%s\n", cgGetLastListing(cgCtx));
   }

   if (!fPrg || !vPrg)
      return false;

   cgD3D9LoadProgram(fPrg, true, 0);
   cgD3D9LoadProgram(vPrg, true, 0);
#endif
   return true;
}
 const char* ProfileLimits::GetProfileString (CGprofile p)
 {
   switch (p)
   {
     case CG_PROFILE_UNKNOWN: return "(unknown)";
     default: return cgGetProfileString (p);
   }
 }
Example #3
0
int createShader ( int n, std::string vname, std::string vfunc, std::string fname, std::string ffunc)
{
#if 0
	char vnbuf[200];
	char vfbuf[200];
	char fnbuf[200];
	char ffbuf[200];
	strcpy ( vnbuf, vname.c_str() );
	strcpy ( vfbuf, vfunc.c_str() );
	strcpy ( fnbuf, fname.c_str() );
	strcpy ( ffbuf, ffunc.c_str() );

	if ( cgContext == 0 ) {
		cgSetErrorCallback( cgErrorCallback );
		cgContext = cgCreateContext();
	}

	// Select profiles
	vert_profile = cgGLGetLatestProfile ( CG_GL_VERTEX );
	cgGLSetOptimalOptions( vert_profile );
	
	frag_profile = cgGLGetLatestProfile ( CG_GL_FRAGMENT );
	cgGLSetOptimalOptions( frag_profile );

	printf ( "Vertex profile:   %s\n", cgGetProfileString(vert_profile) );
	printf ( "Fragment profile: %s\n", cgGetProfileString(frag_profile) );
	printf ( " (See http.developer.nvidia.com/Cg/index_profiles.html)\n");

	//debug.PrintF ( "rend", "Geometry profile: %s\n", cgGetProfileString(vert_profile) );	

	printf ( "Loading VP:       %s\n", vnbuf );
	cgVP = cgCreateProgramFromFile( cgContext, CG_SOURCE, vnbuf, vert_profile, vfbuf, NULL );
	cgGLLoadProgram( cgVP );
	
	printf ( "Loading FP:       %s\n", fnbuf );
	cgFP = cgCreateProgramFromFile( cgContext, CG_SOURCE, fnbuf, frag_profile, ffbuf, NULL );	
	cgGLLoadProgram( cgFP );
	
	cgGLSetManageTextureParameters ( cgContext, CG_FALSE ); 
	cgGLBindProgram ( cgVP );
	cgGLBindProgram ( cgFP );	
#endif
	return 0;
}
Example #4
0
bool Shader::compileSourceCode (CGcontext context,
                                const QString& code, const QString& entryPoint)
{
    d->profile = cgGLGetLatestProfile(d->profileClass);

    qDebug("profile: %s", cgGetProfileString(d->profile));

    cgGLSetOptimalOptions(d->profile);
    d->program = cgCreateProgram(context, CG_SOURCE, code.toLocal8Bit(),
                                 d->profile, entryPoint.toLocal8Bit(), NULL);
    return true;
}
Example #5
0
static void *gl_cg_init(void *data, const char *path)
{
   unsigned i;
   cg_shader_data_t *cg_data = (cg_shader_data_t*)
      calloc(1, sizeof(cg_shader_data_t));

   if (!cg_data)
      return NULL;

#ifdef HAVE_CG_RUNTIME_COMPILER
   cgRTCgcInit();
#endif

   cg_data->cgCtx = cgCreateContext();

   if (!cg_data->cgCtx)
   {
      RARCH_ERR("Failed to create Cg context.\n");
      goto error;
   }

#ifdef RARCH_CG_DEBUG
   cgGLSetDebugMode(CG_TRUE);
   cgSetErrorHandler(cg_error_handler, NULL);
#endif

   cg_data->cgFProf = cgGLGetLatestProfile(CG_GL_FRAGMENT);
   cg_data->cgVProf = cgGLGetLatestProfile(CG_GL_VERTEX);

   if (
         cg_data->cgFProf == CG_PROFILE_UNKNOWN ||
         cg_data->cgVProf == CG_PROFILE_UNKNOWN)
   {
      RARCH_ERR("Invalid profile type\n");
      free(cg_data);
      cg_data = NULL;
      goto error;
   }

   RARCH_LOG("[Cg]: Vertex profile: %s\n",   cgGetProfileString(cg_data->cgVProf));
   RARCH_LOG("[Cg]: Fragment profile: %s\n", cgGetProfileString(cg_data->cgFProf));
   cgGLSetOptimalOptions(cg_data->cgFProf);
   cgGLSetOptimalOptions(cg_data->cgVProf);
   cgGLEnableProfile(cg_data->cgFProf);
   cgGLEnableProfile(cg_data->cgVProf);

   memset(cg_data->cg_alias_define, 0, sizeof(cg_data->cg_alias_define));

   if (path && string_is_equal(path_get_extension(path), "cgp"))
   {
      if (!gl_cg_load_preset(cg_data, path))
         goto error;
   }
   else
   {
      if (!gl_cg_load_plain(cg_data, path))
         goto error;
   }

   cg_data->prg[0].mvp = cgGetNamedParameter(cg_data->prg[0].vprg, "IN.mvp_matrix");
   for (i = 1; i <= cg_data->shader->passes; i++)
      gl_cg_set_program_attributes(cg_data, i);

   /* If we aren't using last pass non-FBO shader, 
    * this shader will be assumed to be "fixed-function".
    *
    * Just use prg[0] for that pass, which will be
    * pass-through. */
   cg_data->prg[cg_data->shader->passes + 1] = cg_data->prg[0]; 

   /* No need to apply Android hack in Cg. */
   cg_data->prg[GL_SHADER_STOCK_BLEND] = cg_data->prg[0];

   cgGLBindProgram(cg_data->prg[1].fprg);
   cgGLBindProgram(cg_data->prg[1].vprg);

   return cg_data;

error:
   gl_cg_destroy_resources(cg_data);
   if (!cg_data)
      free(cg_data);
   return NULL;
}
static bool d3d9_cg_load_program(void *data,
      struct shader_pass *pass,
      const char *prog, bool path_is_file)
{
   const char *list           = NULL;
   char *listing_f            = NULL;
   char *listing_v            = NULL;
   CGprofile vertex_profile   = cgD3D9GetLatestVertexProfile();
   CGprofile fragment_profile = cgD3D9GetLatestPixelProfile();
   const char **fragment_opts = cgD3D9GetOptimalOptions(fragment_profile);
   const char **vertex_opts   = cgD3D9GetOptimalOptions(vertex_profile);
   cg_renderchain_t *chain    = (cg_renderchain_t*)data;
   CGcontext cgCtx            = chain->cgCtx;

   if (
         fragment_profile == CG_PROFILE_UNKNOWN ||
         vertex_profile   == CG_PROFILE_UNKNOWN)
   {
      RARCH_ERR("Invalid profile type\n");
      goto error;
   }

   RARCH_LOG("[D3D9 Cg]: Vertex profile: %s\n",
         cgGetProfileString(vertex_profile));
   RARCH_LOG("[D3D9 Cg]: Fragment profile: %s\n",
         cgGetProfileString(fragment_profile));

   if (path_is_file && !string_is_empty(prog))
      pass->fprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE,
            prog, fragment_profile, "main_fragment", fragment_opts);
   else
      pass->fprg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_d3d9_program,
            fragment_profile, "main_fragment", fragment_opts);

   list = cgGetLastListing(cgCtx);
   if (list)
      listing_f = strdup(list);

   if (path_is_file && !string_is_empty(prog))
      pass->vprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE,
            prog, vertex_profile, "main_vertex", vertex_opts);
   else
      pass->vprg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_d3d9_program,
            vertex_profile, "main_vertex", vertex_opts);

   list = cgGetLastListing(cgCtx);
   if (list)
      listing_v = strdup(list);

   if (!pass->fprg || !pass->vprg)
      goto error;

   cgD3D9LoadProgram(pass->fprg, true, 0);
   cgD3D9LoadProgram(pass->vprg, true, 0);

   free(listing_f);
   free(listing_v);

   return true;

error:
   RARCH_ERR("CG error: %s\n", cgGetErrorString(cgGetError()));
   if (listing_f)
      RARCH_ERR("Fragment:\n%s\n", listing_f);
   else if (listing_v)
      RARCH_ERR("Vertex:\n%s\n", listing_v);
   free(listing_f);
   free(listing_v);

   return false;
}
Example #7
0
void InitCg()
{
	cgSetErrorCallback(CgErrorCallback);
	Context = cgCreateContext();
	VertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
	printf("VertexProfile %s\n", cgGetProfileString(VertexProfile));
	cgGLSetOptimalOptions(VertexProfile);
	FragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
	printf("FragmentProfile %s\n", cgGetProfileString(FragmentProfile));
	cgGLSetOptimalOptions(FragmentProfile);

	VP_DrawSites = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_DrawSites.cg",
							VertexProfile,
							NULL, NULL);
	FP_DrawSites = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_DrawSites.cg",
							FragmentProfile,
							NULL, NULL);
	VP_Flood = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_Flood.cg",
							VertexProfile,
							NULL, NULL);
	FP_Flood = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_Flood.cg",
							FragmentProfile,
							NULL, NULL);
	VP_Scatter = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_Scatter.cg",
							VertexProfile,
							NULL, NULL);
	FP_Scatter = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_Scatter.cg",
							FragmentProfile,
							NULL, NULL);
	VP_DrawNewSites = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_DrawNewSites.cg",
							VertexProfile,
							NULL, NULL);
	FP_DrawNewSites = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_DrawNewSites.cg",
							FragmentProfile,
							NULL, NULL);
	VP_DrawSitesOQ = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_DrawSitesOQ.cg",
							VertexProfile,
							NULL, NULL);
	FP_DrawSitesOQ = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_DrawSitesOQ.cg",
							FragmentProfile,
							NULL, NULL);
	VP_FinalRender = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_FinalRender.cg",
							VertexProfile,
							NULL, NULL);
	FP_FinalRender = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_FinalRender.cg",
							FragmentProfile,
							NULL, NULL);
	VP_ComputeEnergy = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_ComputeEnergy.cg",
							VertexProfile,
							NULL, NULL);
	FP_ComputeEnergy = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_ComputeEnergy.cg",
							FragmentProfile,
							NULL, NULL);
	VP_Deduction = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_Deduction.cg",
							VertexProfile,
							NULL, NULL);
	FP_Deduction = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_Deduction.cg",
							FragmentProfile,
							NULL, NULL);
	VP_ComputeEnergyCentroid = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_ComputeEnergyCentroid.cg",
							VertexProfile,
							NULL, NULL);
	FP_ComputeEnergyCentroid = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_ComputeEnergyCentroid.cg",
							FragmentProfile,
							NULL, NULL);
	VP_ScatterCentroid = cgCreateProgramFromFile(Context,
							CG_SOURCE, "VP_ScatterCentroid.cg",
							VertexProfile,
							NULL, NULL);
	FP_ScatterCentroid = cgCreateProgramFromFile(Context,
							CG_SOURCE, "FP_ScatterCentroid.cg",
							FragmentProfile,
							NULL, NULL);

	if(VP_DrawSites != NULL)
	{
		cgGLLoadProgram(VP_DrawSites);
	}
	if(FP_DrawSites != NULL)
	{
		cgGLLoadProgram(FP_DrawSites);
	}

	if(VP_Flood != NULL)
	{
		cgGLLoadProgram(VP_Flood);

		// Bind parameters to give access to variables in the shader
		VP_Flood_Steplength = cgGetNamedParameter(VP_Flood, "steplength");
		VP_Flood_Size = cgGetNamedParameter(VP_Flood, "size");
	}
	if(FP_Flood != NULL)
	{
		cgGLLoadProgram(FP_Flood);
	}

	if(VP_Scatter != NULL)
	{
		cgGLLoadProgram(VP_Scatter);

		// Bind parameters to give access to variables in the shader
		VP_Scatter_Size = cgGetNamedParameter(VP_Scatter, "size");
	}
	if(FP_Scatter != NULL)
	{
		cgGLLoadProgram(FP_Scatter);
	}

	if(VP_DrawNewSites != NULL)
	{
		cgGLLoadProgram(VP_DrawNewSites);

		// Bind parameters to give access to variables in the shader
		VP_DrawNewSites_Size = cgGetNamedParameter(VP_DrawNewSites, "size");
	}
	if(FP_DrawNewSites != NULL)
	{
		cgGLLoadProgram(FP_DrawNewSites);
	}

	if(VP_DrawSitesOQ != NULL)
	{
		cgGLLoadProgram(VP_DrawSitesOQ);

		// Bind parameters to give access to variables in the shader
		VP_DrawSitesOQ_Size = cgGetNamedParameter(VP_DrawSitesOQ, "size");
	}
	if(FP_DrawSitesOQ != NULL)
	{
		cgGLLoadProgram(FP_DrawSitesOQ);
	}

	if(VP_FinalRender != NULL)
	{
		cgGLLoadProgram(VP_FinalRender);
	}
	if(FP_FinalRender != NULL)
	{
		cgGLLoadProgram(FP_FinalRender);

		// Bind parameters to give access to variables in the shader
		FP_FinalRender_Size = cgGetNamedParameter(FP_FinalRender, "size");
	}

	if(VP_ComputeEnergy != NULL)
	{
		cgGLLoadProgram(VP_ComputeEnergy);
	}
	if(FP_ComputeEnergy != NULL)
	{
		cgGLLoadProgram(FP_ComputeEnergy);

		// Bind parameters to give access to variables in the shader
		FP_ComputeEnergy_Size = cgGetNamedParameter(FP_ComputeEnergy, "size");
	}

	if(VP_Deduction != NULL)
	{
		cgGLLoadProgram(VP_Deduction);
	}
	if(FP_Deduction != NULL)
	{
		cgGLLoadProgram(FP_Deduction);
	}

	if(VP_ComputeEnergyCentroid != NULL)
	{
		cgGLLoadProgram(VP_ComputeEnergyCentroid);
	}
	if(FP_ComputeEnergyCentroid != NULL)
	{
		cgGLLoadProgram(FP_ComputeEnergyCentroid);

		// Bind parameters to give access to variables in the shader
		FP_ComputeEnergyCentroid_Size = cgGetNamedParameter(FP_ComputeEnergyCentroid, "size");
	}

	if(VP_ScatterCentroid != NULL)
	{
		cgGLLoadProgram(VP_ScatterCentroid);

		// Bind parameters to give access to variables in the shader
		VP_ScatterCentroid_Size = cgGetNamedParameter(VP_ScatterCentroid, "size");
	}
	if(FP_ScatterCentroid != NULL)
	{
		cgGLLoadProgram(FP_ScatterCentroid);
	}
}
Example #8
0
static bool gl_cg_init(const char *path)
{
   unsigned i;
#ifdef HAVE_CG_RUNTIME_COMPILER
   cgRTCgcInit();
#endif

   if (!cgCtx)
      cgCtx = cgCreateContext();

   if (cgCtx == NULL)
   {
      RARCH_ERR("Failed to create Cg context\n");
      return false;
   }

#ifdef RARCH_CG_DEBUG
   cgGLSetDebugMode(CG_TRUE);
   cgSetErrorHandler(cg_error_handler, NULL);
#endif

   cgFProf = cgGLGetLatestProfile(CG_GL_FRAGMENT);
   cgVProf = cgGLGetLatestProfile(CG_GL_VERTEX);
   if (cgFProf == CG_PROFILE_UNKNOWN || cgVProf == CG_PROFILE_UNKNOWN)
   {
      RARCH_ERR("Invalid profile type\n");
      goto error;
   }
#ifndef HAVE_RGL
   RARCH_LOG("[Cg]: Vertex profile: %s\n", cgGetProfileString(cgVProf));
   RARCH_LOG("[Cg]: Fragment profile: %s\n", cgGetProfileString(cgFProf));
#endif
   cgGLSetOptimalOptions(cgFProf);
   cgGLSetOptimalOptions(cgVProf);
   cgGLEnableProfile(cgFProf);
   cgGLEnableProfile(cgVProf);

   if (path && strcmp(path_get_extension(path), "cgp") == 0)
   {
      if (!load_preset(path))
         goto error;
   }
   else
   {
      if (!load_plain(path))
         goto error;
   }

   prg[0].mvp = cgGetNamedParameter(prg[0].vprg, "IN.mvp_matrix");
   for (i = 1; i <= cg_shader->passes; i++)
      set_program_attributes(i);

   // If we aren't using last pass non-FBO shader, 
   // this shader will be assumed to be "fixed-function".
   // Just use prg[0] for that pass, which will be
   // pass-through.
   prg[cg_shader->passes + 1] = prg[0]; 

   // No need to apply Android hack in Cg.
   prg[GL_SHADER_STOCK_BLEND] = prg[0];

   cgGLBindProgram(prg[1].fprg);
   cgGLBindProgram(prg[1].vprg);

   cg_active = true;
   return true;

error:
   gl_cg_deinit();
   return false;
}
bool csShaderGLCGCommon::DefaultLoadProgram (iShaderProgramCG* cgResolve,
  const char* programStr, ProgramType _type,
  const ProfileLimitsPair& customLimitsPair, uint flags)
{
  if (!programStr || !*programStr) return false;

  const ProfileLimits& customLimits = (_type == progVP) ?
    customLimitsPair.vp : customLimitsPair.fp;
  CGGLenum type = (_type == progVP) ? CG_GL_VERTEX : CG_GL_FRAGMENT;

  size_t i;
  csString augmentedProgramStr = GetAugmentedProgram (programStr,
    (flags & loadFlagUnusedV2FForInit) != 0);
    
  programStr = augmentedProgramStr;
  CGprofile profile = customLimits.profile;
  CS::PluginCommon::ShaderProgramPluginGL::HardwareVendor vendor =
    customLimits.vendor;

  if (shaderPlug->doVerbose || shaderPlug->doVerbosePrecache)
  {
    shaderPlug->Report (CS_REPORTER_SEVERITY_NOTIFY,
      "Cg %s program %s: using profile %s[%d]", GetProgramType(),
      CS::Quote::Single (description.GetData ()),
      cgGetProfileString (profile), profile);
  }

  ArgumentArray args;
  shaderPlug->GetProfileCompilerArgs (GetProgramType(), profile, 
    customLimitsPair,
    vendor,
    (flags & loadIgnoreConfigProgramOpts) ? csGLShader_CG::argsNoConfig
      : csGLShader_CG::argsAll, args);
  for (i = 0; i < compilerArgs.GetSize(); i++) 
    args.Push (compilerArgs[i]);
  programPositionInvariant = false;
  for (i = 0; i < args.GetSize(); ) 
  {
    if (strcmp (args[i], "-posinv") == 0)
    {
      if (profile >= CG_PROFILE_GPU_VP)
      {
	/* Work around Cg 2.0 and above (including 3.0) bug: it emits
	   "OPTION ARB_position_invariant;" AND computes result.position in
	   the VP - doing both is verboten.
	   Affected are the GP4VP and higher profiles.
	   Remedy: remove -posinv argument 
	 */
	args.DeleteIndex (i);
	continue;
      }
      programPositionInvariant = true;
    }
    i++;
  }
  customLimits.ToCgOptions (args);
  args.Push (0);
 
  if (program)
  {
    cgDestroyProgram (program);
  }
  shaderPlug->SetCompiledSource (programStr);
  shaderPlug->SetIgnoreErrors (true);
  program = cgCreateProgram (shaderPlug->context, 
    CG_SOURCE, programStr, 
    profile, !entrypoint.IsEmpty() ? entrypoint : "main", args.GetArray());
  shaderPlug->SetIgnoreErrors (false);
  
  if (!(flags & loadIgnoreErrors)) shaderPlug->PrintAnyListing();

  if (!program)
  {
    shaderPlug->SetCompiledSource (0);
    /*if (shaderPlug->debugDump)
    {
      EnsureDumpFile();
      WriteAdditionalDumpInfo ("Failed program source", programStr);
    }*/
    return false;
  }
  programProfile = cgGetProgramProfile (program);

  GetParamsFromVmapConstants();
  if (flags & loadApplyVmap)
    GetParamsFromVmap();

  if (flags & loadIgnoreErrors) shaderPlug->SetIgnoreErrors (true);
  cgCompileProgram (program);
  if (flags & loadIgnoreErrors)
    shaderPlug->SetIgnoreErrors (false);
  else
    shaderPlug->PrintAnyListing();
  
  if (flags & loadApplyVmap)
    GetPostCompileParamProps ();

  if (flags & loadLoadToGL)
  {
    cgGetError(); // Clear error
    cgGLLoadProgram (program);
    if (!(flags & loadIgnoreErrors)) shaderPlug->PrintAnyListing();
    if ((cgGetError() != CG_NO_ERROR)
      || !cgGLIsProgramLoaded (program)) 
    {
      if (shaderPlug->debugDump)
	DoDebugDump();

      if (shaderPlug->doVerbose
	  && (((type == CG_GL_VERTEX) && (profile >= CG_PROFILE_ARBVP1))
	    || ((type == CG_GL_FRAGMENT) && (profile >= CG_PROFILE_ARBFP1))))
      {
	csString err = (char*)glGetString (GL_PROGRAM_ERROR_STRING_ARB);
	if (!err.IsEmpty())
	  shaderPlug->Report (CS_REPORTER_SEVERITY_WARNING,
	    "OpenGL error string: %s", err.GetData());
      }

      shaderPlug->SetCompiledSource (0);
      return false;
    }
  }

  if (shaderPlug->debugDump)
    DoDebugDump();
  
  shaderPlug->SetCompiledSource (0);

  bool result = true;
  if (programType == progFP)
  {
    int numVaryings = 0;
    CGparameter param = cgGetFirstLeafParameter (program, CG_PROGRAM);
    while (param)
    {
      if (cgIsParameterUsed (param, program)
	&& cgIsParameterReferenced (param))
      {
	const CGenum var = cgGetParameterVariability (param);
	if (var == CG_VARYING)
	  numVaryings++;
      }
  
      param = cgGetNextLeafParameter (param);
    }
    
    /* WORKAROUND: Even NVs G80 doesn't support passing more than 16 attribs
       into an FP, yet Cg happily generates code that uses more (and GL falls 
       back to SW).
       So manually check the number of varying inputs and reject more than 16.
       
       @@@ This should be at least configurable
     */
    const int maxNumVaryings = 16;
    if (numVaryings > maxNumVaryings)
    {
      if (shaderPlug->doVerbose || shaderPlug->doVerbosePrecache)
      {
	shaderPlug->Report (CS_REPORTER_SEVERITY_NOTIFY,
	  "Discarding compiled program for having too much varyings "
	  "(%d, limit is %d)",
	  numVaryings, maxNumVaryings);
      }
      cgDestroyProgram (program);
      program = 0;
      result = false;
    }
  }
  if (!result && !debugFN.IsEmpty())
  {
    csRef<iVFS> vfs = csQueryRegistry<iVFS> (objectReg);
    vfs->DeleteFile (debugFN);
  }
  return result;
}
Example #10
0
	IOGLBaseShader::IOGLBaseShader(CRefPtr<COGLDevice> pDevice, const Graphic::ShaderType uType, const Graphic::ShaderVersion uVersion, CGenum uSourceType, CGprofile uProfile, const CString& strSource, const CString& strEntryPoint) :
		m_uVersion(uVersion),
		m_uType(uType),
		m_uProgram(0),
		m_bBinded(false),
		Manage::IManagedObject<COGLDevice, IOGLBaseShader>(pDevice)
	{
		if(!cgIsProfileSupported(uProfile)){
			throw Exception::CInvalidArgumentException(L"uProfile", String::FromANSI(reinterpret_cast<const int8*>(cgGetProfileString(uProfile))),
				L"Unsupported shader profile.", CR_INFO());
		}

		auto szSource = String::ToANSI(strSource);
		auto szEntryPoint = String::ToANSI(strEntryPoint);

		this->m_uProgram = cgCreateProgram(this->GetParent()->GetCGC().Get(), uSourceType, reinterpret_cast<const char*>(szSource.GetPointer()), uProfile, reinterpret_cast<const char*>(szEntryPoint.GetPointer()), 0);
		if(!cgIsProgram(this->m_uProgram)){
			CR_THROW(L"Failed to create shader program.");
		}

		cgCompileProgram(this->m_uProgram);
		if(!cgIsProgramCompiled(this->m_uProgram)){
			CR_THROW(L"Failed to compile program.");
		}

		cgGLLoadProgram(this->m_uProgram);
	}
Example #11
0
String FragmentShaderCg::GetProfile() const
{
	return cgGetProfileString(m_pCgProfile);
}
Example #12
0
static bool d3d9_cg_load_program(void *data,
      void *fragment_data, void *vertex_data, const char *prog, bool path_is_file)
{
   const char *list           = NULL;
   char *listing_f            = NULL;
   char *listing_v            = NULL;
   CGprogram *fPrg            = (CGprogram*)fragment_data;
   CGprogram *vPrg            = (CGprogram*)vertex_data;
   CGprofile vertex_profile   = cgD3D9GetLatestVertexProfile();
   CGprofile fragment_profile = cgD3D9GetLatestPixelProfile();
   const char **fragment_opts = cgD3D9GetOptimalOptions(fragment_profile);
   const char **vertex_opts   = cgD3D9GetOptimalOptions(vertex_profile);
   cg_renderchain_t *cg_data  = (cg_renderchain_t*)data;

   RARCH_LOG("[D3D Cg]: Vertex profile: %s\n", cgGetProfileString(vertex_profile));
   RARCH_LOG("[D3D Cg]: Fragment profile: %s\n", cgGetProfileString(fragment_profile));

   if (path_is_file && !string_is_empty(prog))
      *fPrg = cgCreateProgramFromFile(cg_data->cgCtx, CG_SOURCE,
            prog, fragment_profile, "main_fragment", fragment_opts);
   else
      *fPrg = cgCreateProgram(cg_data->cgCtx, CG_SOURCE, stock_cg_d3d9_program,
            fragment_profile, "main_fragment", fragment_opts);

   list = cgGetLastListing(cg_data->cgCtx);
   if (list)
      listing_f = strdup(list);

   if (path_is_file && !string_is_empty(prog))
      *vPrg = cgCreateProgramFromFile(cg_data->cgCtx, CG_SOURCE,
            prog, vertex_profile, "main_vertex", vertex_opts);
   else
      *vPrg = cgCreateProgram(cg_data->cgCtx, CG_SOURCE, stock_cg_d3d9_program,
            vertex_profile, "main_vertex", vertex_opts);

   list = cgGetLastListing(cg_data->cgCtx);
   if (list)
      listing_v = strdup(list);

   if (!fPrg || !vPrg)
      goto error;

   cgD3D9LoadProgram(*fPrg, true, 0);
   cgD3D9LoadProgram(*vPrg, true, 0);

   free(listing_f);
   free(listing_v);

   return true;

error:
   RARCH_ERR("CG error: %s\n", cgGetErrorString(cgGetError()));
   if (listing_f)
      RARCH_ERR("Fragment:\n%s\n", listing_f);
   else if (listing_v)
      RARCH_ERR("Vertex:\n%s\n", listing_v);
   free(listing_f);
   free(listing_v);

   return false;
}