SlimDX (DirectX10) - 如何更改纹理中的纹素?

发布于 2024-07-30 05:06:49 字数 228 浏览 7 评论 0原文

我尝试更改已加载的纹理的纹理像素。

我的假设是使用Texture2D::Map 和UnMap 函数,但是当我更改给定DataRectangle 的数据时没有任何变化。

我需要一个简单的示例,例如创建 128x128 的纹理,每侧都有从黑到白的渐变。

谢谢

ps:Direct3D 10 C++ 示例也可能有帮助,SlimDX 只是一个包装器,并且具有几乎相同的功能。

I try to change the texels of a Texture which is already loaded.

My assumption was to use the Texture2D::Map and UnMap functions, but there is no change when I change the data of given DataRectangle.

I need a simple example like, creating a texture of 128x128 with a gradient from black to white from each side.

Thx

ps: A Direct3D 10 C++ example may also help, SlimDX is only a wrapper and has nearly complete the same functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左岸枫 2024-08-06 05:06:50

确保遵循“资源使用限制”部分中的规则:
MSDN:D3D10_USAGE

Make sure you follow the rules in the "Resource Usage Restrictions" section:
MSDN: D3D10_USAGE

分开我的手 2024-08-06 05:06:50
    public void NewData(byte[] newData)
    {
        DataRectangle mappedTex = null;
        //assign and lock the resource
        mappedTex = pTexture.Map(0, D3D10.MapMode.WriteDiscard, D3D10.MapFlags.None);
        // if unable to hold texture
        if (!mappedTex.Data.CanWrite)
        {
            throw new ApplicationException("Cannot Write to the Texture");
        }
        // write new data to the texture
        mappedTex.Data.WriteRange<byte>(newData);
        // unlock the resource
        pTexture.Unmap(0);
        if (samplerflag)
            temptex = newData;
    }

这会覆盖每个新帧上的缓冲区,您可能需要使用 D3D10.MapMode.readwrite 或其他东西,如果您只想写入一个纹理元素,

您还需要使用其他写入函数之一在特定点写入数据矩形

    public void NewData(byte[] newData)
    {
        DataRectangle mappedTex = null;
        //assign and lock the resource
        mappedTex = pTexture.Map(0, D3D10.MapMode.WriteDiscard, D3D10.MapFlags.None);
        // if unable to hold texture
        if (!mappedTex.Data.CanWrite)
        {
            throw new ApplicationException("Cannot Write to the Texture");
        }
        // write new data to the texture
        mappedTex.Data.WriteRange<byte>(newData);
        // unlock the resource
        pTexture.Unmap(0);
        if (samplerflag)
            temptex = newData;
    }

this overwrites the buffer on every new frame, you may want to use a D3D10.MapMode.readwrite or something if ur only trying to write one texel

you will also need to write to the datarectangle in a specific point using one of the other write functions

旧话新听 2024-08-06 05:06:49

这是我的 D3D10 2D 纹理加载器

bool D3D10Texture::Init( GFXHandler* pHandler, unsigned int usage, unsigned int width, unsigned int height, unsigned int textureType, bool bMipmapped, void* pTextureData )
{
    mMipmapped      = bMipmapped;

    //SetData( pHandler, 0 );

    D3D10Handler* pD3DHandler   = (D3D10Handler*)pHandler;
    ID3D10Device* pDevice       = pD3DHandler->GetDevice();

    DXGI_SAMPLE_DESC dxgiSampleDesc;
    dxgiSampleDesc.Count    = 1;
    dxgiSampleDesc.Quality  = 0;

    D3D10_USAGE d3d10Usage;
    if ( usage & RU_All_Dynamic )   d3d10Usage  = D3D10_USAGE_DYNAMIC;
    else                            d3d10Usage  = D3D10_USAGE_DEFAULT;

    //unsigned int cpuAccess    = D3D10_CPU_ACCESS_WRITE;
    //if ( (usage & RU_Buffer_WriteOnly) == 0 ) cpuAccess |= D3D10_CPU_ACCESS_READ;

    unsigned int cpuAccess = 0;
    if ( !pTextureData )
    {
        cpuAccess   = D3D10_CPU_ACCESS_WRITE;
        //if ( (usage & RU_Buffer_WriteOnly) == 0 ) cpuAccess |= D3D10_CPU_ACCESS_READ;
    }

    unsigned int bindFlags  = D3D10_BIND_SHADER_RESOURCE;
    if ( usage & RU_Texture_RenderTarget )  bindFlags |= D3D10_BIND_RENDER_TARGET;

    unsigned int miscFlags  = 0;
    if ( usage & RU_Texture_AutoGenMipmap ) miscFlags |= D3D10_RESOURCE_MISC_GENERATE_MIPS;

    D3D10_TEXTURE2D_DESC d3d10Texture2DDesc;
    d3d10Texture2DDesc.Width            = width;
    d3d10Texture2DDesc.Height           = height;
    d3d10Texture2DDesc.MipLevels        = GetNumMipMaps( width, height, bMipmapped );
    d3d10Texture2DDesc.ArraySize        = 1;
    d3d10Texture2DDesc.Format           = GetD3DFormat( (TextureTypes)textureType );
    d3d10Texture2DDesc.SampleDesc       = dxgiSampleDesc;
    d3d10Texture2DDesc.Usage            = d3d10Usage;
    d3d10Texture2DDesc.BindFlags        = D3D10_BIND_SHADER_RESOURCE;
    d3d10Texture2DDesc.CPUAccessFlags   = cpuAccess;
    d3d10Texture2DDesc.MiscFlags        = miscFlags;

    //D3D10_SUBRESOURCE_DATA d3d10SubResourceData;
    //d3d10SubResourceData.pSysMem          = pTextureData;
    //d3d10SubResourceData.SysMemPitch      = GetPitch( width, (TextureTypes)textureType );
    //d3d10SubResourceData.SysMemSlicePitch = 0;

    D3D10_SUBRESOURCE_DATA* pSubResourceData    = NULL;
    if ( pTextureData ) 
    {
        pSubResourceData    = new D3D10_SUBRESOURCE_DATA[d3d10Texture2DDesc.MipLevels];

        char* pTexPos       = (char*)pTextureData;
        unsigned int pitch  = GetPitch( width, (TextureTypes)textureType );

        unsigned int count  = 0;
        unsigned int max    = d3d10Texture2DDesc.MipLevels;
        while( count < max )
        {
            pSubResourceData[count].pSysMem             = pTexPos;
            pSubResourceData[count].SysMemPitch         = pitch;
            pSubResourceData[count].SysMemSlicePitch    = 0;

            pTexPos += pitch * height;
            pitch   >>= 1;
            count++;
        }
    }

    if ( FAILED( pDevice->CreateTexture2D( &d3d10Texture2DDesc, pSubResourceData, &mpTexture ) ) )
    {
        return false;
    }

    if ( pSubResourceData )
    {
        delete[] pSubResourceData;
        pSubResourceData    = NULL;
    }

    mWidth  = width;
    mHeight = height;
    mFormat = (TextureTypes)textureType;

    mpTexture->AddRef();
    mpTexture->Release();

    D3D10_SHADER_RESOURCE_VIEW_DESC d3d10ShaderResourceViewDesc;
    d3d10ShaderResourceViewDesc.Format                      = d3d10Texture2DDesc.Format;
    d3d10ShaderResourceViewDesc.ViewDimension               = D3D10_SRV_DIMENSION_TEXTURE2D;
    d3d10ShaderResourceViewDesc.Texture2D.MostDetailedMip   = 0;
    d3d10ShaderResourceViewDesc.Texture2D.MipLevels         = GetNumMipMaps( width, height, bMipmapped );

    if ( FAILED( pDevice->CreateShaderResourceView( mpTexture, &d3d10ShaderResourceViewDesc, &mpView ) ) )
    {
        return false;
    }

    ResourceRecorder::Instance()->AddResource( this );
    return true;
}

使用该功能,您所需要做的就是将白色纹理传递到黑色纹理。 例如,要编写 256x256 纹理,每条水平线都比前一行亮,以下代码将起作用

int* pTexture = new int[256 * 256];
int count = 0;
while( count < 256 )
{
    int count2 = 0;
    while( count2 < 256 )
    {
        pTexture[(count * 256) + count2] = 0xff000000 | (count << 16) | (count << 8) | count;
        count2++;
    }
    count++;
}

This is my D3D10 2D texture loader

bool D3D10Texture::Init( GFXHandler* pHandler, unsigned int usage, unsigned int width, unsigned int height, unsigned int textureType, bool bMipmapped, void* pTextureData )
{
    mMipmapped      = bMipmapped;

    //SetData( pHandler, 0 );

    D3D10Handler* pD3DHandler   = (D3D10Handler*)pHandler;
    ID3D10Device* pDevice       = pD3DHandler->GetDevice();

    DXGI_SAMPLE_DESC dxgiSampleDesc;
    dxgiSampleDesc.Count    = 1;
    dxgiSampleDesc.Quality  = 0;

    D3D10_USAGE d3d10Usage;
    if ( usage & RU_All_Dynamic )   d3d10Usage  = D3D10_USAGE_DYNAMIC;
    else                            d3d10Usage  = D3D10_USAGE_DEFAULT;

    //unsigned int cpuAccess    = D3D10_CPU_ACCESS_WRITE;
    //if ( (usage & RU_Buffer_WriteOnly) == 0 ) cpuAccess |= D3D10_CPU_ACCESS_READ;

    unsigned int cpuAccess = 0;
    if ( !pTextureData )
    {
        cpuAccess   = D3D10_CPU_ACCESS_WRITE;
        //if ( (usage & RU_Buffer_WriteOnly) == 0 ) cpuAccess |= D3D10_CPU_ACCESS_READ;
    }

    unsigned int bindFlags  = D3D10_BIND_SHADER_RESOURCE;
    if ( usage & RU_Texture_RenderTarget )  bindFlags |= D3D10_BIND_RENDER_TARGET;

    unsigned int miscFlags  = 0;
    if ( usage & RU_Texture_AutoGenMipmap ) miscFlags |= D3D10_RESOURCE_MISC_GENERATE_MIPS;

    D3D10_TEXTURE2D_DESC d3d10Texture2DDesc;
    d3d10Texture2DDesc.Width            = width;
    d3d10Texture2DDesc.Height           = height;
    d3d10Texture2DDesc.MipLevels        = GetNumMipMaps( width, height, bMipmapped );
    d3d10Texture2DDesc.ArraySize        = 1;
    d3d10Texture2DDesc.Format           = GetD3DFormat( (TextureTypes)textureType );
    d3d10Texture2DDesc.SampleDesc       = dxgiSampleDesc;
    d3d10Texture2DDesc.Usage            = d3d10Usage;
    d3d10Texture2DDesc.BindFlags        = D3D10_BIND_SHADER_RESOURCE;
    d3d10Texture2DDesc.CPUAccessFlags   = cpuAccess;
    d3d10Texture2DDesc.MiscFlags        = miscFlags;

    //D3D10_SUBRESOURCE_DATA d3d10SubResourceData;
    //d3d10SubResourceData.pSysMem          = pTextureData;
    //d3d10SubResourceData.SysMemPitch      = GetPitch( width, (TextureTypes)textureType );
    //d3d10SubResourceData.SysMemSlicePitch = 0;

    D3D10_SUBRESOURCE_DATA* pSubResourceData    = NULL;
    if ( pTextureData ) 
    {
        pSubResourceData    = new D3D10_SUBRESOURCE_DATA[d3d10Texture2DDesc.MipLevels];

        char* pTexPos       = (char*)pTextureData;
        unsigned int pitch  = GetPitch( width, (TextureTypes)textureType );

        unsigned int count  = 0;
        unsigned int max    = d3d10Texture2DDesc.MipLevels;
        while( count < max )
        {
            pSubResourceData[count].pSysMem             = pTexPos;
            pSubResourceData[count].SysMemPitch         = pitch;
            pSubResourceData[count].SysMemSlicePitch    = 0;

            pTexPos += pitch * height;
            pitch   >>= 1;
            count++;
        }
    }

    if ( FAILED( pDevice->CreateTexture2D( &d3d10Texture2DDesc, pSubResourceData, &mpTexture ) ) )
    {
        return false;
    }

    if ( pSubResourceData )
    {
        delete[] pSubResourceData;
        pSubResourceData    = NULL;
    }

    mWidth  = width;
    mHeight = height;
    mFormat = (TextureTypes)textureType;

    mpTexture->AddRef();
    mpTexture->Release();

    D3D10_SHADER_RESOURCE_VIEW_DESC d3d10ShaderResourceViewDesc;
    d3d10ShaderResourceViewDesc.Format                      = d3d10Texture2DDesc.Format;
    d3d10ShaderResourceViewDesc.ViewDimension               = D3D10_SRV_DIMENSION_TEXTURE2D;
    d3d10ShaderResourceViewDesc.Texture2D.MostDetailedMip   = 0;
    d3d10ShaderResourceViewDesc.Texture2D.MipLevels         = GetNumMipMaps( width, height, bMipmapped );

    if ( FAILED( pDevice->CreateShaderResourceView( mpTexture, &d3d10ShaderResourceViewDesc, &mpView ) ) )
    {
        return false;
    }

    ResourceRecorder::Instance()->AddResource( this );
    return true;
}

With that function all you need to do is pass in the whit to black texture. For example to write a 256x256 textue with each horizontal line being one brighter than the previous line the following code will work

int* pTexture = new int[256 * 256];
int count = 0;
while( count < 256 )
{
    int count2 = 0;
    while( count2 < 256 )
    {
        pTexture[(count * 256) + count2] = 0xff000000 | (count << 16) | (count << 8) | count;
        count2++;
    }
    count++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文