【问题标题】:Textured quad rendering in pixel stripes像素条纹中的纹理四边形渲染
【发布时间】:2014-06-12 22:01:23
【问题描述】:

我有以下.fx 文件:

Texture2D texture2d;

SamplerState linearSampler
{
  Filter = MIN_MAG_MIP_LINEAR;
  AddressU = Clamp;
  AddressV = Clamp;
};

struct VS_IN
{
  float4 pos : POSITION;
  float3 uvq : TEXCOORD0;
};

struct PS_IN
{
  float4 pos : SV_POSITION;
  float3 uvq : TEXCOORD0;
};

PS_IN VS(VS_IN input)
{
  PS_IN output = (PS_IN)0;

  output.pos = float4(input.pos.xyz, 1.0);
  output.uvq = input.uvq;

  return output;
}

float4 PS(PS_IN input) : SV_Target
{
  return texture2d.Sample(linearSampler, input.uvq.xy);
}

technique10 Render
{
  pass P0
  {
    SetVertexShader(CompileShader(vs_4_0, VS()));
    SetGeometryShader(NULL);
    SetPixelShader(CompileShader(ps_4_0, PS()));
  }
}

(注意:uvqfloat3 是有原因的,q 稍后会被使用,但现在被忽略了)。

当我使用此效果对四边形(左侧的纹理图像)进行纹理映射时,我得到了右侧的渲染:像素完全来自纹理,但它们被奇怪地采样并以条纹形式重复。

坐标没什么特别的,完整的纹理:

public Vertex[] vertices = {
  new Vertex() { Position = new D3.Vector4F(-0.7f, -0.5f, 0, 0), UVQ = new D3.Vector3F(0f, 1f, 0) },
  new Vertex() { Position = new D3.Vector4F(-0.4f,  0.6f, 0, 0), UVQ = new D3.Vector3F(0f, 0f, 0) },
  new Vertex() { Position = new D3.Vector4F( 0.5f, -0.5f, 0, 0), UVQ = new D3.Vector3F(1f, 1f, 0) },
  new Vertex() { Position = new D3.Vector4F( 0.3f,  0.9f, 0, 0), UVQ = new D3.Vector3F(1f, 0f, 0) },
};

纹理已使用 Code Pack API 教程中的 TextureLoader.cs 辅助例程读取。

var TextureVariable = DxEffect.GetVariableByName("texture2d").AsShaderResource;
var stream = Application.GetResourceStream(MakePackUri("Resources/image.png"));
TextureVariable.Resource = DXUtil.TextureLoader.LoadTexture(DxDevice, stream.Stream);

输入布局如下:

var layout = new D3D.InputElementDescription[] {
  new D3D.InputElementDescription() { SemanticName = "POSITION", SemanticIndex = 0, Format = GX.Format.R32G32B32A32Float, AlignedByteOffset = 0, InputSlot = 0, InputSlotClass = D3D.InputClassification.PerVertexData, InstanceDataStepRate = 0 },
  new D3D.InputElementDescription() { SemanticName = "TEXCOORD", SemanticIndex = 0, Format = GX.Format.R32G32B32Float, AlignedByteOffset = 16, InputSlot = 0, InputSlotClass = D3D.InputClassification.PerVertexData, InstanceDataStepRate = 0 }
};
var DxPassDesc = DxTechnique.GetPassByIndex(0).Description;
var DxLayout = DxDevice.CreateInputLayout(layout, DxPassDesc.InputAssemblerInputSignature, DxPassDesc.InputAssemblerInputSignatureSize);
DxDevice.IA.InputLayout = DxLayout;

var vertex = new VertexArray();
var DxVertexBufferDescription = new D3D.BufferDescription() { BindingOptions = D3D.BindingOptions.VertexBuffer, CpuAccessOptions = D3D.CpuAccessOptions.None, MiscellaneousResourceOptions = D3D.MiscellaneousResourceOptions.None, Usage = D3D.Usage.Default, ByteWidth = (uint)Marshal.SizeOf(vertex) };
IntPtr vertexData = Marshal.AllocCoTaskMem(Marshal.SizeOf(vertex));
Marshal.StructureToPtr(vertex, vertexData, false);
var DxVertexBufferInitData = new D3D.SubresourceData() { SystemMemory = vertexData, SystemMemoryPitch = 0, SystemMemorySlicePitch = 0 };
var DxVertices = DxDevice.CreateBuffer(DxVertexBufferDescription, DxVertexBufferInitData);
uint stride = (uint)Marshal.SizeOf(typeof(Vertex));
uint offset = 0;
DxDevice.IA.SetVertexBuffers(0, new D3D.D3DBuffer[] { DxVertices }, new uint[] { stride }, new uint[] { offset });
Marshal.FreeCoTaskMem(vertexData);

【问题讨论】:

  • 贴图坐标好像不对,能贴一下你的程序代码吗?
  • 现在添加到问题中,我忘了提及它,因为它并没有什么特别之处:像往常一样从零到一的完整纹理。
  • 您可以发布用于将顶点缓冲区绑定到着色器的输入布局吗?可能来自一些无效的步幅。
  • 添加了输入布局。

标签: directx hlsl windows-api-code-pack


【解决方案1】:

解决了。最大纹理大小似乎是 512(尽管 MS 的描述似乎建议 DX10 Texture2D 的值更大)。嗯……

【讨论】:

    猜你喜欢
    • 2021-01-03
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多