当前位置: 首页>>代码示例>>C++>>正文


C++ ShaderCode::Write方法代码示例

本文整理汇总了C++中ShaderCode::Write方法的典型用法代码示例。如果您正苦于以下问题:C++ ShaderCode::Write方法的具体用法?C++ ShaderCode::Write怎么用?C++ ShaderCode::Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ShaderCode的用法示例。


在下文中一共展示了ShaderCode::Write方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: SetShader

bool LineGeometryShader::SetShader(u32 components, float lineWidth,
	float texOffset, float vpWidth, float vpHeight, const bool* texOffsetEnable)
{
	if (!m_ready)
		return false;

	// Make sure geometry shader for "components" is available
	ComboMap::iterator shaderIt = m_shaders.find(components);
	if (shaderIt == m_shaders.end())
	{
		// Generate new shader. Warning: not thread-safe.
		static char buffer[16384];
		ShaderCode code;
		code.SetBuffer(buffer);
		GenerateVSOutputStructForGS(code, API_D3D);
		code.Write("\n%s", LINE_GS_COMMON);

		std::stringstream numTexCoordsStream;
		numTexCoordsStream << xfmem.numTexGen.numTexGens;

		INFO_LOG(VIDEO, "Compiling line geometry shader for components 0x%.08X (num texcoords %d)",
			components, xfmem.numTexGen.numTexGens);

		const std::string& numTexCoordsStr = numTexCoordsStream.str();
		D3D_SHADER_MACRO macros[] = {
			{ "NUM_TEXCOORDS", numTexCoordsStr.c_str() },
			{ nullptr, nullptr }
		};
		ID3D11GeometryShader* newShader = D3D::CompileAndCreateGeometryShader(code.GetBuffer(), macros);
		if (!newShader)
		{
			WARN_LOG(VIDEO, "Line geometry shader for components 0x%.08X failed to compile", components);
			// Add dummy shader to prevent trying to compile again
			m_shaders[components] = nullptr;
			return false;
		}

		shaderIt = m_shaders.insert(std::make_pair(components, newShader)).first;
	}

	if (shaderIt != m_shaders.end())
	{
		if (shaderIt->second)
		{
			D3D11_MAPPED_SUBRESOURCE map;
			HRESULT hr = D3D::context->Map(m_paramsBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
			if (SUCCEEDED(hr))
			{
				LineGSParams* params = (LineGSParams*)map.pData;
				params->LineWidth = lineWidth;

				params->TexOffset = texOffset;
				params->VpWidth = vpWidth;
				params->VpHeight = vpHeight;
				for (int i = 0; i < 8; ++i)
					params->TexOffsetEnable[i] = texOffsetEnable[i] ? 1.f : 0.f;

				D3D::context->Unmap(m_paramsBuffer, 0);
			}
			else
				ERROR_LOG(VIDEO, "Failed to map line gs params buffer");

			DEBUG_LOG(VIDEO, "Line params: width %f, texOffset %f, vpWidth %f, vpHeight %f",
				lineWidth, texOffset, vpWidth, vpHeight);

			D3D::context->GSSetShader(shaderIt->second, nullptr, 0);
			D3D::context->GSSetConstantBuffers(0, 1, &m_paramsBuffer);

			return true;
		}
		else
			return false;
	}
	else
		return false;
}
开发者ID:Llumex03,项目名称:dolphin,代码行数:76,代码来源:LineGeometryShader.cpp

示例2: GenerateLightingShaderCode

// vertex shader
// lights/colors
// materials name is I_MATERIALS in vs and I_PMATERIALS in ps
// inColorName is color in vs and colors_ in ps
// dest is o.colors_ in vs and colors_ in ps
void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_data, int components,
                                const char* inColorName, const char* dest)
{
  for (unsigned int j = 0; j < xfmem.numChan.numColorChans; j++)
  {
    object.Write("{\n");

    bool colormatsource = !!(uid_data.matsource & (1 << j));
    if (colormatsource)  // from vertex
    {
      if (components & (VB_HAS_COL0 << j))
        object.Write("int4 mat = int4(round(%s%d * 255.0));\n", inColorName, j);
      else if (components & VB_HAS_COL0)
        object.Write("int4 mat = int4(round(%s0 * 255.0));\n", inColorName);
      else
        object.Write("int4 mat = int4(255, 255, 255, 255);\n");
    }
    else  // from color
    {
      object.Write("int4 mat = %s[%d];\n", I_MATERIALS, j + 2);
    }

    if (uid_data.enablelighting & (1 << j))
    {
      if (uid_data.ambsource & (1 << j))  // from vertex
      {
        if (components & (VB_HAS_COL0 << j))
          object.Write("lacc = int4(round(%s%d * 255.0));\n", inColorName, j);
        else if (components & VB_HAS_COL0)
          object.Write("lacc = int4(round(%s0 * 255.0));\n", inColorName);
        else
          // TODO: this isn't verified. Here we want to read the ambient from the vertex,
          // but the vertex itself has no color. So we don't know which value to read.
          // Returning 1.0 is the same as disabled lightning, so this could be fine
          object.Write("lacc = int4(255, 255, 255, 255);\n");
      }
      else  // from color
      {
        object.Write("lacc = %s[%d];\n", I_MATERIALS, j);
      }
    }
    else
    {
      object.Write("lacc = int4(255, 255, 255, 255);\n");
    }

    // check if alpha is different
    bool alphamatsource = !!(uid_data.matsource & (1 << (j + 2)));
    if (alphamatsource != colormatsource)
    {
      if (alphamatsource)  // from vertex
      {
        if (components & (VB_HAS_COL0 << j))
          object.Write("mat.w = int(round(%s%d.w * 255.0));\n", inColorName, j);
        else if (components & VB_HAS_COL0)
          object.Write("mat.w = int(round(%s0.w * 255.0));\n", inColorName);
        else
          object.Write("mat.w = 255;\n");
      }
      else  // from color
      {
        object.Write("mat.w = %s[%d].w;\n", I_MATERIALS, j + 2);
      }
    }

    if (uid_data.enablelighting & (1 << (j + 2)))
    {
      if (uid_data.ambsource & (1 << (j + 2)))  // from vertex
      {
        if (components & (VB_HAS_COL0 << j))
          object.Write("lacc.w = int(round(%s%d.w * 255.0));\n", inColorName, j);
        else if (components & VB_HAS_COL0)
          object.Write("lacc.w = int(round(%s0.w * 255.0));\n", inColorName);
        else
          // TODO: The same for alpha: We want to read from vertex, but the vertex has no color
          object.Write("lacc.w = 255;\n");
      }
      else  // from color
      {
        object.Write("lacc.w = %s[%d].w;\n", I_MATERIALS, j);
      }
    }
    else
    {
      object.Write("lacc.w = 255;\n");
    }

    if (uid_data.enablelighting & (1 << j))  // Color lights
    {
      for (int i = 0; i < 8; ++i)
        if (uid_data.light_mask & (1 << (i + 8 * j)))
          GenerateLightShader(object, uid_data, i, j, false);
    }
    if (uid_data.enablelighting & (1 << (j + 2)))  // Alpha lights
    {
//.........这里部分代码省略.........
开发者ID:Antidote,项目名称:dolphin,代码行数:101,代码来源:LightingShaderGen.cpp

示例3: GenerateLightShader

static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_data, int index,
                                int litchan_index, bool alpha)
{
  const char* swizzle = alpha ? "a" : "rgb";
  const char* swizzle_components = (alpha) ? "" : "3";

  int attnfunc = (uid_data.attnfunc >> (2 * litchan_index)) & 0x3;
  int diffusefunc = (uid_data.diffusefunc >> (2 * litchan_index)) & 0x3;

  switch (attnfunc)
  {
  case LIGHTATTN_NONE:
  case LIGHTATTN_DIR:
    object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index));
    object.Write("attn = 1.0;\n");
    object.Write("if (length(ldir) == 0.0)\n\t ldir = _norm0;\n");
    break;
  case LIGHTATTN_SPEC:
    object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index));
    object.Write("attn = (dot(_norm0, ldir) >= 0.0) ? max(0.0, dot(_norm0, " LIGHT_DIR
                 ".xyz)) : 0.0;\n",
                 LIGHT_DIR_PARAMS(index));
    object.Write("cosAttn = " LIGHT_COSATT ".xyz;\n", LIGHT_COSATT_PARAMS(index));
    object.Write("distAttn = %s(" LIGHT_DISTATT ".xyz);\n",
                 (diffusefunc == LIGHTDIF_NONE) ? "" : "normalize", LIGHT_DISTATT_PARAMS(index));
    object.Write("attn = max(0.0f, dot(cosAttn, float3(1.0, attn, attn*attn))) / dot(distAttn, "
                 "float3(1.0, attn, attn*attn));\n");
    break;
  case LIGHTATTN_SPOT:
    object.Write("ldir = " LIGHT_POS ".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(index));
    object.Write("dist2 = dot(ldir, ldir);\n"
                 "dist = sqrt(dist2);\n"
                 "ldir = ldir / dist;\n"
                 "attn = max(0.0, dot(ldir, " LIGHT_DIR ".xyz));\n",
                 LIGHT_DIR_PARAMS(index));
    // attn*attn may overflow
    object.Write("attn = max(0.0, " LIGHT_COSATT ".x + " LIGHT_COSATT ".y*attn + " LIGHT_COSATT
                 ".z*attn*attn) / dot(" LIGHT_DISTATT ".xyz, float3(1.0,dist,dist2));\n",
                 LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index),
                 LIGHT_DISTATT_PARAMS(index));
    break;
  }

  switch (diffusefunc)
  {
  case LIGHTDIF_NONE:
    object.Write("lacc.%s += int%s(round(attn * float%s(" LIGHT_COL ")));\n", swizzle,
                 swizzle_components, swizzle_components, LIGHT_COL_PARAMS(index, swizzle));
    break;
  case LIGHTDIF_SIGN:
  case LIGHTDIF_CLAMP:
    object.Write("lacc.%s += int%s(round(attn * %sdot(ldir, _norm0)) * float%s(" LIGHT_COL ")));\n",
                 swizzle, swizzle_components, diffusefunc != LIGHTDIF_SIGN ? "max(0.0," : "(",
                 swizzle_components, LIGHT_COL_PARAMS(index, swizzle));
    break;
  default:
    _assert_(0);
  }

  object.Write("\n");
}
开发者ID:Antidote,项目名称:dolphin,代码行数:61,代码来源:LightingShaderGen.cpp


注:本文中的ShaderCode::Write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。