Created
March 14, 2012 16:38
-
-
Save toshinoritakata/2037719 to your computer and use it in GitHub Desktop.
DirectX11でメモリからテクスチャを作成する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HRESULT loadTextureFromMemory(unsigned char* image, int w, int h, LPCSTR shaderTextureName, ID3D11Device* pd3dDevice, ID3DX11Effect* effect) | |
{ | |
D3D11_TEXTURE2D_DESC desc; | |
D3D11_SUBRESOURCE_DATA initialData; | |
ID3D11Texture2D* texture = NULL; | |
ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC)); | |
desc.Width = w; | |
desc.Height = h; | |
desc.MipLevels = 1; | |
desc.ArraySize = 1; | |
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; | |
desc.SampleDesc.Count = 1; | |
desc.SampleDesc.Quality = 0; | |
desc.Usage = D3D11_USAGE_DEFAULT; | |
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; | |
desc.CPUAccessFlags = 0; | |
desc.MiscFlags = 0; | |
ZeroMemory(&initialData, sizeof(D3D11_SUBRESOURCE_DATA)); | |
initialData.pSysMem = image; | |
initialData.SysMemPitch = w*4; | |
HRESULT hr; | |
V_RETURN(pd3dDevice->CreateTexture2D(&desc, &initialData, &texture)); | |
if (texture) { | |
ID3D11ShaderResourceView* textureRview = NULL; | |
ID3DX11EffectShaderResourceVariable* textureRVar = NULL; | |
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; | |
ZeroMemory( &SRVDesc, sizeof(SRVDesc) ); | |
SRVDesc.Format = desc.Format; | |
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; | |
SRVDesc.Texture2D.MostDetailedMip = 0; | |
SRVDesc.Texture2D.MipLevels = 1; | |
V_RETURN(pd3dDevice->CreateShaderResourceView(texture, &SRVDesc, &textureRview)); | |
textureRVar = effect->GetVariableByName( shaderTextureName )->AsShaderResource(); | |
V_RETURN(textureRVar->SetResource( textureRview )); | |
SAFE_RELEASE( texture ); | |
SAFE_RELEASE( textureRview ); | |
} | |
return S_OK; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment