// block dimensions : widthStride, heightStride
// texture dims : width, height, x offset, y offset
static void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType)
{
	// left, top, of source rectangle within source texture
	// width of the destination rectangle, scale_factor (1 or 2)
	WRITE(p, "uniform int4 position;\n");

	int blkW = TexDecoder_GetBlockWidthInTexels(format);
	int blkH = TexDecoder_GetBlockHeightInTexels(format);
	int samples = GetEncodedSampleCount(format);

	if (ApiType == API_OPENGL)
	{
		WRITE(p, "#define samp0 samp9\n");
		WRITE(p, "SAMPLER_BINDING(9) uniform sampler2DArray samp0;\n");

		WRITE(p, "  out vec4 ocol0;\n");
		WRITE(p, "void main()\n");
	}
	else // D3D
	{
		WRITE(p,"sampler samp0 : register(s0);\n");
		WRITE(p, "Texture2D Tex0 : register(t0);\n");

		WRITE(p,"void main(\n");
		WRITE(p,"  out float4 ocol0 : SV_Target)\n");
	}

	WRITE(p, "{\n"
	"  int2 sampleUv;\n"
	"  int2 uv1 = int2(gl_FragCoord.xy);\n"
	);

	WRITE(p, "  int y_block_position = uv1.y & %d;\n", ~(blkH - 1));
	WRITE(p, "  int y_offset_in_block = uv1.y & %d;\n", blkH - 1);
	WRITE(p, "  int x_virtual_position = (uv1.x << %d) + y_offset_in_block * position.z;\n", IntLog2(samples));
	WRITE(p, "  int x_block_position = (x_virtual_position >> %d) & %d;\n", IntLog2(blkH), ~(blkW - 1));
	if (samples == 1)
	{
		// 32 bit textures (RGBA8 and Z24) are stored in 2 cache line increments
		WRITE(p, "  bool first = 0 == (x_virtual_position & %d);\n", 8 * samples); // first cache line, used in the encoders
		WRITE(p, "  x_virtual_position = x_virtual_position << 1;\n");
	}
	WRITE(p, "  int x_offset_in_block = x_virtual_position & %d;\n", blkW - 1);
	WRITE(p, "  int y_offset = (x_virtual_position >> %d) & %d;\n", IntLog2(blkW), blkH - 1);

	WRITE(p, "  sampleUv.x = x_offset_in_block + x_block_position;\n");
	WRITE(p, "  sampleUv.y = y_block_position + y_offset;\n");

	WRITE(p, "  float2 uv0 = float2(sampleUv);\n");                // sampleUv is the sample position in (int)gx_coords
	WRITE(p, "  uv0 += float2(0.5, 0.5);\n");                      // move to center of pixel
	WRITE(p, "  uv0 *= float(position.w);\n");                     // scale by two if needed (also move to pixel borders so that linear filtering will average adjacent pixel)
	WRITE(p, "  uv0 += float2(position.xy);\n");                   // move to copied rect
	WRITE(p, "  uv0 /= float2(%d, %d);\n", EFB_WIDTH, EFB_HEIGHT); // normalize to [0:1]
	if (ApiType == API_OPENGL)                                     // ogl has to flip up and down
	{
		WRITE(p, "  uv0.y = 1.0-uv0.y;\n");
	}

	WRITE(p, "  float sample_offset = float(position.w) / float(%d);\n", EFB_WIDTH);
}
// block dimensions : widthStride, heightStride
// texture dims : width, height, x offset, y offset
void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType)
{
	// left, top, of source rectangle within source texture
	// width of the destination rectangle, scale_factor (1 or 2)
	WRITE(p, "uniform int4 position;\n");

	int blkW = TexDecoder_GetBlockWidthInTexels(format);
	int blkH = TexDecoder_GetBlockHeightInTexels(format);
	int samples = GetEncodedSampleCount(format);
	// 32 bit textures (RGBA8 and Z24) are store in 2 cache line increments
	int factor = samples == 1 ? 2 : 1;
	if (ApiType == API_OPENGL)
	{
		WRITE(p, "#define samp0 samp9\n");
		WRITE(p, "uniform sampler2D samp0;\n");

		WRITE(p, "  out vec4 ocol0;\n");
		WRITE(p, "void main()\n");
	}
	else // D3D
	{
		WRITE(p,"sampler samp0 : register(s0);\n");
		WRITE(p, "Texture2D Tex0 : register(t0);\n");

		WRITE(p,"void main(\n");
		WRITE(p,"  out float4 ocol0 : SV_Target)\n");
	}

	WRITE(p, "{\n"
	"  int2 sampleUv;\n"
	"  int2 uv1 = int2(gl_FragCoord.xy);\n"
	"  float2 uv0 = float2(0.0, 0.0);\n"
	);

	WRITE(p, "  uv1.x = uv1.x * %d;\n", samples);

	WRITE(p, "  int yl = uv1.y / %d;\n", blkH);
	WRITE(p, "  int yb = yl * %d;\n", blkH);
	WRITE(p, "  int yoff = uv1.y - yb;\n");
	WRITE(p, "  int xp = uv1.x + yoff * position.z;\n");
	WRITE(p, "  int xel = xp / %d;\n", samples == 1 ? factor : blkW);
	WRITE(p, "  int xb = xel / %d;\n", blkH);
	WRITE(p, "  int xoff = xel - xb * %d;\n", blkH);
	WRITE(p, "  int xl =  uv1.x * %d / %d;\n", factor, blkW);
	WRITE(p, "  int xib = uv1.x * %d - xl * %d;\n", factor, blkW);
	WRITE(p, "  int halfxb = xb / %d;\n", factor);

	WRITE(p, "  sampleUv.x = xib + halfxb * %d;\n", blkW);
	WRITE(p, "  sampleUv.y = yb + xoff;\n");
}
// block dimensions : widthStride, heightStride 
// texture dims : width, height, x offset, y offset
void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType)
{
	WRITE(p, "uniform float4 blkDims : register(c%d);\n", C_COLORMATRIX);
	WRITE(p, "uniform float4 textureDims : register(c%d);\n", C_COLORMATRIX + 1);	

    float blkW = (float)TexDecoder_GetBlockWidthInTexels(format);
	float blkH = (float)TexDecoder_GetBlockHeightInTexels(format);
	float samples = (float)GetEncodedSampleCount(format);
	if(ApiType == API_OPENGL)
	{
		WRITE(p,"uniform samplerRECT samp0 : register(s0);\n");
	}
	else if (ApiType & API_D3D9)
	{
		WRITE(p,"uniform sampler samp0 : register(s0);\n");
	}
	else
	{
		WRITE(p,"sampler samp0 : register(s0);\n");
		WRITE(p, "Texture2D Tex0 : register(t0);\n");		
	}

	
	WRITE(p,"void main(\n");
	if(ApiType != API_D3D11)
	{
		WRITE(p,"  out float4 ocol0 : COLOR0,\n");
	}
	else
	{
		WRITE(p,"  out float4 ocol0 : SV_Target,\n");
	}
	
	WRITE(p,"  in float2 uv0 : TEXCOORD0)\n"
	"{\n"    
    "  float2 sampleUv;\n"
	"  float2 uv1 = floor(uv0);\n");

	WRITE(p, "  uv1.x = uv1.x * %f;\n", samples);

	WRITE(p, "  float xl =  floor(uv1.x / %f);\n", blkW);
	WRITE(p, "  float xib = uv1.x - (xl * %f);\n", blkW);
	WRITE(p, "  float yl = floor(uv1.y / %f);\n", blkH);
	WRITE(p, "  float yb = yl * %f;\n", blkH);
	WRITE(p, "  float yoff = uv1.y - yb;\n");
	WRITE(p, "  float xp = uv1.x + (yoff * textureDims.x);\n");
	WRITE(p, "  float xel = floor(xp / %f);\n", blkW);
	WRITE(p, "  float xb = floor(xel / %f);\n", blkH);
	WRITE(p, "  float xoff = xel - (xb * %f);\n", blkH);

	WRITE(p, "  sampleUv.x = xib + (xb * %f);\n", blkW);
	WRITE(p, "  sampleUv.y = yb + xoff;\n");

	WRITE(p, "  sampleUv = sampleUv * blkDims.xy;\n");

	if(ApiType == API_OPENGL)
		WRITE(p,"  sampleUv.y = textureDims.y - sampleUv.y;\n");
	
	WRITE(p, "  sampleUv = sampleUv + textureDims.zw;\n");

	if(ApiType != API_OPENGL)
	{
		WRITE(p, "  sampleUv = sampleUv + float2(0.0f,1.0f);\n");// still to determine the reason for this
		WRITE(p, "  sampleUv = sampleUv / blkDims.zw;\n");
	}		
}
示例#4
0
// block dimensions : widthStride, heightStride
// texture dims : width, height, x offset, y offset
static void WriteSwizzler(char*& p, u32 format, APIType ApiType)
{
  // left, top, of source rectangle within source texture
  // width of the destination rectangle, scale_factor (1 or 2)
  WRITE(p, "uniform int4 position;\n");

  int blkW = TexDecoder_GetBlockWidthInTexels(format);
  int blkH = TexDecoder_GetBlockHeightInTexels(format);
  int samples = GetEncodedSampleCount(format);

  if (ApiType == APIType::OpenGL)
  {
    WRITE(p, "#define samp0 samp9\n");
    WRITE(p, "SAMPLER_BINDING(9) uniform sampler2DArray samp0;\n");

    WRITE(p, "  out vec4 ocol0;\n");
    WRITE(p, "void main()\n");
    WRITE(p, "{\n"
             "  int2 sampleUv;\n"
             "  int2 uv1 = int2(gl_FragCoord.xy);\n");
  }
  else  // D3D
  {
    WRITE(p, "sampler samp0 : register(s0);\n");
    WRITE(p, "Texture2DArray Tex0 : register(t0);\n");

    WRITE(p, "void main(\n");
    WRITE(p, "  out float4 ocol0 : SV_Target, in float4 rawpos : SV_Position)\n");
    WRITE(p, "{\n"
             "  int2 sampleUv;\n"
             "  int2 uv1 = int2(rawpos.xy);\n");
  }

  WRITE(p, "  int x_block_position = (uv1.x >> %d) << %d;\n", IntLog2(blkH * blkW / samples),
        IntLog2(blkW));
  WRITE(p, "  int y_block_position = uv1.y << %d;\n", IntLog2(blkH));
  if (samples == 1)
  {
    // With samples == 1, we write out pairs of blocks; one A8R8, one G8B8.
    WRITE(p, "  bool first = (uv1.x & %d) == 0;\n", blkH * blkW / 2);
    samples = 2;
  }
  WRITE(p, "  int offset_in_block = uv1.x & %d;\n", (blkH * blkW / samples) - 1);
  WRITE(p, "  int y_offset_in_block = offset_in_block >> %d;\n", IntLog2(blkW / samples));
  WRITE(p, "  int x_offset_in_block = (offset_in_block & %d) << %d;\n", (blkW / samples) - 1,
        IntLog2(samples));

  WRITE(p, "  sampleUv.x = x_block_position + x_offset_in_block;\n");
  WRITE(p, "  sampleUv.y = y_block_position + y_offset_in_block;\n");

  WRITE(p,
        "  float2 uv0 = float2(sampleUv);\n");  // sampleUv is the sample position in (int)gx_coords
  WRITE(p, "  uv0 += float2(0.5, 0.5);\n");     // move to center of pixel
  WRITE(p, "  uv0 *= float(position.w);\n");  // scale by two if needed (also move to pixel borders
                                              // so that linear filtering will average adjacent
                                              // pixel)
  WRITE(p, "  uv0 += float2(position.xy);\n");                    // move to copied rect
  WRITE(p, "  uv0 /= float2(%d, %d);\n", EFB_WIDTH, EFB_HEIGHT);  // normalize to [0:1]
  if (ApiType == APIType::OpenGL)                                 // ogl has to flip up and down
  {
    WRITE(p, "  uv0.y = 1.0-uv0.y;\n");
  }

  WRITE(p, "  float sample_offset = float(position.w) / float(%d);\n", EFB_WIDTH);
}
// block dimensions : widthStride, heightStride
// texture dims : width, height, x offset, y offset
static void WriteSwizzler(char*& p, EFBCopyFormat format, APIType ApiType)
{
  // left, top, of source rectangle within source texture
  // width of the destination rectangle, scale_factor (1 or 2)
  if (ApiType == APIType::Vulkan)
    WRITE(p, "layout(std140, push_constant) uniform PCBlock { int4 position; } PC;\n");
  else
    WRITE(p, "uniform int4 position;\n");

  // Alpha channel in the copy is set to 1 the EFB format does not have an alpha channel.
  WRITE(p, "float4 RGBA8ToRGB8(float4 src)\n");
  WRITE(p, "{\n");
  WRITE(p, "  return float4(src.xyz, 1.0);\n");
  WRITE(p, "}\n");

  WRITE(p, "float4 RGBA8ToRGBA6(float4 src)\n");
  WRITE(p, "{\n");
  WRITE(p, "  int4 val = int4(src * 255.0) >> 2;\n");
  WRITE(p, "  return float4(val) / 63.0;\n");
  WRITE(p, "}\n");

  WRITE(p, "float4 RGBA8ToRGB565(float4 src)\n");
  WRITE(p, "{\n");
  WRITE(p, "  int4 val = int4(src * 255.0);\n");
  WRITE(p, "  val = int4(val.r >> 3, val.g >> 2, val.b >> 3, 1);\n");
  WRITE(p, "  return float4(val) / float4(31.0, 63.0, 31.0, 1.0);\n");
  WRITE(p, "}\n");

  int blkW = TexDecoder_GetEFBCopyBlockWidthInTexels(format);
  int blkH = TexDecoder_GetEFBCopyBlockHeightInTexels(format);
  int samples = GetEncodedSampleCount(format);

  if (ApiType == APIType::OpenGL)
  {
    WRITE(p, "#define samp0 samp9\n");
    WRITE(p, "SAMPLER_BINDING(9) uniform sampler2DArray samp0;\n");

    WRITE(p, "FRAGMENT_OUTPUT_LOCATION(0) out vec4 ocol0;\n");
    WRITE(p, "void main()\n");
    WRITE(p, "{\n"
             "  int2 sampleUv;\n"
             "  int2 uv1 = int2(gl_FragCoord.xy);\n");
  }
  else if (ApiType == APIType::Vulkan)
  {
    WRITE(p, "SAMPLER_BINDING(0) uniform sampler2DArray samp0;\n");
    WRITE(p, "FRAGMENT_OUTPUT_LOCATION(0) out vec4 ocol0;\n");

    WRITE(p, "void main()\n");
    WRITE(p, "{\n"
             "  int2 sampleUv;\n"
             "  int2 uv1 = int2(gl_FragCoord.xy);\n"
             "  int4 position = PC.position;\n");
  }
  else  // D3D
  {
    WRITE(p, "sampler samp0 : register(s0);\n");
    WRITE(p, "Texture2DArray Tex0 : register(t0);\n");

    WRITE(p, "void main(\n");
    WRITE(p, "  out float4 ocol0 : SV_Target, in float4 rawpos : SV_Position)\n");
    WRITE(p, "{\n"
             "  int2 sampleUv;\n"
             "  int2 uv1 = int2(rawpos.xy);\n");
  }

  WRITE(p, "  int x_block_position = (uv1.x >> %d) << %d;\n", IntLog2(blkH * blkW / samples),
        IntLog2(blkW));
  WRITE(p, "  int y_block_position = uv1.y << %d;\n", IntLog2(blkH));
  if (samples == 1)
  {
    // With samples == 1, we write out pairs of blocks; one A8R8, one G8B8.
    WRITE(p, "  bool first = (uv1.x & %d) == 0;\n", blkH * blkW / 2);
    samples = 2;
  }
  WRITE(p, "  int offset_in_block = uv1.x & %d;\n", (blkH * blkW / samples) - 1);
  WRITE(p, "  int y_offset_in_block = offset_in_block >> %d;\n", IntLog2(blkW / samples));
  WRITE(p, "  int x_offset_in_block = (offset_in_block & %d) << %d;\n", (blkW / samples) - 1,
        IntLog2(samples));

  WRITE(p, "  sampleUv.x = x_block_position + x_offset_in_block;\n");
  WRITE(p, "  sampleUv.y = y_block_position + y_offset_in_block;\n");

  WRITE(p,
        "  float2 uv0 = float2(sampleUv);\n");  // sampleUv is the sample position in (int)gx_coords
  WRITE(p, "  uv0 += float2(0.5, 0.5);\n");     // move to center of pixel
  WRITE(p, "  uv0 *= float(position.w);\n");  // scale by two if needed (also move to pixel borders
                                              // so that linear filtering will average adjacent
                                              // pixel)
  WRITE(p, "  uv0 += float2(position.xy);\n");                    // move to copied rect
  WRITE(p, "  uv0 /= float2(%d, %d);\n", EFB_WIDTH, EFB_HEIGHT);  // normalize to [0:1]
  if (ApiType == APIType::OpenGL)                                 // ogl has to flip up and down
  {
    WRITE(p, "  uv0.y = 1.0-uv0.y;\n");
  }

  WRITE(p, "  float sample_offset = float(position.w) / float(%d);\n", EFB_WIDTH);
}
// block dimensions : widthStride, heightStride
// texture dims : width, height, x offset, y offset
static void WriteSwizzler(char*& p, const EFBCopyParams& params, EFBCopyFormat format,
                          APIType ApiType)
{
  WriteHeader(p, ApiType);
  WriteSampleFunction(p, params, ApiType);

  if (ApiType == APIType::OpenGL || ApiType == APIType::Vulkan)
  {
    WRITE(p, "void main()\n");
    WRITE(p, "{\n"
             "  int2 sampleUv;\n"
             "  int2 uv1 = int2(gl_FragCoord.xy);\n");
  }
  else  // D3D
  {
    WRITE(p, "void main(\n");
    WRITE(p, "  in float3 v_tex0 : TEXCOORD0,\n");
    WRITE(p, "  in float4 rawpos : SV_Position,\n");
    WRITE(p, "  out float4 ocol0 : SV_Target)\n");
    WRITE(p, "{\n"
             "  int2 sampleUv;\n"
             "  int2 uv1 = int2(rawpos.xy);\n");
  }

  int blkW = TexDecoder_GetEFBCopyBlockWidthInTexels(format);
  int blkH = TexDecoder_GetEFBCopyBlockHeightInTexels(format);
  int samples = GetEncodedSampleCount(format);

  WRITE(p, "  int x_block_position = (uv1.x >> %d) << %d;\n", IntLog2(blkH * blkW / samples),
        IntLog2(blkW));
  WRITE(p, "  int y_block_position = uv1.y << %d;\n", IntLog2(blkH));
  if (samples == 1)
  {
    // With samples == 1, we write out pairs of blocks; one A8R8, one G8B8.
    WRITE(p, "  bool first = (uv1.x & %d) == 0;\n", blkH * blkW / 2);
    samples = 2;
  }
  WRITE(p, "  int offset_in_block = uv1.x & %d;\n", (blkH * blkW / samples) - 1);
  WRITE(p, "  int y_offset_in_block = offset_in_block >> %d;\n", IntLog2(blkW / samples));
  WRITE(p, "  int x_offset_in_block = (offset_in_block & %d) << %d;\n", (blkW / samples) - 1,
        IntLog2(samples));

  WRITE(p, "  sampleUv.x = x_block_position + x_offset_in_block;\n");
  WRITE(p, "  sampleUv.y = y_block_position + y_offset_in_block;\n");

  WRITE(p,
        "  float2 uv0 = float2(sampleUv);\n");  // sampleUv is the sample position in (int)gx_coords
  WRITE(p, "  uv0 += float2(0.5, 0.5);\n");     // move to center of pixel
  WRITE(p, "  uv0 *= float(position.w);\n");  // scale by two if needed (also move to pixel borders
                                              // so that linear filtering will average adjacent
                                              // pixel)
  WRITE(p, "  uv0 += float2(position.xy);\n");                    // move to copied rect
  WRITE(p, "  uv0 /= float2(%d, %d);\n", EFB_WIDTH, EFB_HEIGHT);  // normalize to [0:1]
  WRITE(p, "  uv0 /= float2(1, y_scale);\n");                     // apply the y scaling
  if (ApiType == APIType::OpenGL)                                 // ogl has to flip up and down
  {
    WRITE(p, "  uv0.y = 1.0-uv0.y;\n");
  }

  WRITE(p, "  float2 pixel_size = float2(position.w, position.w) / float2(%d, %d);\n", EFB_WIDTH,
        EFB_HEIGHT);
}