2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "TextureDX.h"
22 #include "windowing/WindowingFactory.h"
23 #include "utils/log.h"
27 /************************************************************************/
29 /************************************************************************/
30 CDXTexture::CDXTexture(unsigned int width, unsigned int height, unsigned int format)
31 : CBaseTexture(width, height, format)
35 CDXTexture::~CDXTexture()
37 DestroyTextureObject();
40 void CDXTexture::CreateTextureObject()
42 D3DFORMAT format = D3DFMT_UNKNOWN;
53 case XB_FMT_DXT5_YCoCg:
58 format = D3DFMT_A8R8G8B8;
67 m_texture.Create(m_textureWidth, m_textureHeight, 1, g_Windowing.DefaultD3DUsage(), format, g_Windowing.DefaultD3DPool());
70 void CDXTexture::DestroyTextureObject()
75 void CDXTexture::LoadToGPU()
79 // nothing to load - probably same image (no change)
83 if (m_texture.Get() == NULL)
85 CreateTextureObject();
86 if (m_texture.Get() == NULL)
88 CLog::Log(LOGDEBUG, "CDXTexture::CDXTexture: Error creating new texture for size %d x %d", m_textureWidth, m_textureHeight);
94 if (m_texture.LockRect( 0, &lr, NULL, D3DLOCK_DISCARD ))
96 unsigned char *dst = (unsigned char *)lr.pBits;
97 unsigned char *src = m_pixels;
98 unsigned int dstPitch = lr.Pitch;
99 unsigned int srcPitch = GetPitch();
100 unsigned int minPitch = std::min(srcPitch, dstPitch);
102 unsigned int rows = GetRows();
103 if (m_format == XB_FMT_RGB8)
105 for (unsigned int y = 0; y < rows; y++)
107 unsigned char *dst2 = dst;
108 unsigned char *src2 = src;
109 for (unsigned int x = 0; x < srcPitch / 3; x++, dst2 += 4, src2 += 3)
120 else if (srcPitch == dstPitch)
122 memcpy(dst, src, srcPitch * rows);
126 for (unsigned int y = 0; y < rows; y++)
128 memcpy(dst, src, minPitch);
136 CLog::Log(LOGERROR, __FUNCTION__" - failed to lock texture");
138 m_texture.UnlockRect(0);
143 m_loadedToGPU = true;
146 void CDXTexture::BindToUnit(unsigned int unit)
148 LPDIRECT3DDEVICE9 p3DDevice = g_Windowing.Get3DDevice();
149 p3DDevice->SetTexture( unit, m_texture.Get() );
150 p3DDevice->SetSamplerState( unit, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
151 p3DDevice->SetSamplerState( unit, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
152 p3DDevice->SetSamplerState( unit, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
153 p3DDevice->SetSamplerState( unit, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );