2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "D3DBase.h"
|
|
|
|
#include "D3DTexture.h"
|
|
|
|
|
|
|
|
namespace D3D
|
|
|
|
{
|
|
|
|
|
2010-04-14 13:57:16 +00:00
|
|
|
LPDIRECT3DTEXTURE9 CreateTexture2D(const u8* buffer, const int width, const int height, const int pitch, D3DFORMAT fmt, bool swap_r_b, int levels)
|
2008-12-26 17:02:46 +00:00
|
|
|
{
|
|
|
|
u32* pBuffer = (u32*)buffer;
|
|
|
|
LPDIRECT3DTEXTURE9 pTexture;
|
|
|
|
|
|
|
|
// crazy bitmagic, sorry :)
|
|
|
|
bool isPow2 = !((width&(width-1)) || (height&(height-1)));
|
2009-02-13 14:14:45 +00:00
|
|
|
bool bExpand = false;
|
|
|
|
|
2009-09-01 19:48:45 +00:00
|
|
|
if (fmt == D3DFMT_A8P8) {
|
2009-02-13 14:14:45 +00:00
|
|
|
fmt = D3DFMT_A8L8;
|
|
|
|
bExpand = true;
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2008-12-26 17:02:46 +00:00
|
|
|
HRESULT hr;
|
2009-09-13 17:46:33 +00:00
|
|
|
// TODO(ector): Allow mipmaps for non-pow textures on newer cards?
|
|
|
|
// TODO(ector): Use the game-specified mipmaps?
|
2010-04-14 13:57:16 +00:00
|
|
|
if (levels > 0)
|
|
|
|
hr = dev->CreateTexture(width, height, levels, 0, fmt, D3DPOOL_MANAGED, &pTexture, NULL);
|
2008-12-26 17:02:46 +00:00
|
|
|
else
|
|
|
|
hr = dev->CreateTexture(width, height, 0, D3DUSAGE_AUTOGENMIPMAP, fmt, D3DPOOL_MANAGED, &pTexture, NULL);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-09-13 17:46:33 +00:00
|
|
|
if (FAILED(hr))
|
2008-12-26 17:02:46 +00:00
|
|
|
return 0;
|
|
|
|
int level = 0;
|
|
|
|
D3DLOCKED_RECT Lock;
|
2009-09-13 17:46:33 +00:00
|
|
|
pTexture->LockRect(level, &Lock, NULL, 0);
|
|
|
|
switch (fmt)
|
2008-12-26 17:02:46 +00:00
|
|
|
{
|
2009-02-12 13:54:08 +00:00
|
|
|
case D3DFMT_L8:
|
|
|
|
case D3DFMT_A8:
|
2009-02-14 09:04:40 +00:00
|
|
|
case D3DFMT_A4L4:
|
2009-02-12 13:54:08 +00:00
|
|
|
{
|
2009-09-01 19:48:45 +00:00
|
|
|
const u8 *pIn = buffer;
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u8* pBits = ((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
memcpy(pBits, pIn, width);
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
2009-02-12 13:54:08 +00:00
|
|
|
}
|
2009-09-01 19:48:45 +00:00
|
|
|
break;
|
2009-02-12 22:32:33 +00:00
|
|
|
case D3DFMT_R5G6B5:
|
|
|
|
{
|
|
|
|
const u16 *pIn = (u16*)buffer;
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u16* pBits = (u16*)((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
memcpy(pBits, pIn, width * 2);
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-02-12 13:54:08 +00:00
|
|
|
case D3DFMT_A8L8:
|
|
|
|
{
|
2009-09-01 19:48:45 +00:00
|
|
|
if (bExpand) { // I8
|
2009-02-13 14:14:45 +00:00
|
|
|
const u8 *pIn = buffer;
|
|
|
|
// TODO(XK): Find a better way that does not involve either unpacking
|
|
|
|
// or downsampling (i.e. A4L4)
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u8* pBits = ((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
for(int i = 0; i < width * 2; i += 2) {
|
|
|
|
pBits[i] = pIn[i / 2];
|
|
|
|
pBits[i + 1] = pIn[i / 2];
|
|
|
|
}
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
|
|
|
} else { // IA8
|
|
|
|
const u16 *pIn = (u16*)buffer;
|
|
|
|
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u16* pBits = (u16*)((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
memcpy(pBits, pIn, width * 2);
|
|
|
|
pIn += pitch;
|
2009-02-12 13:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-12-26 17:02:46 +00:00
|
|
|
case D3DFMT_A8R8G8B8:
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-05-29 09:32:44 +00:00
|
|
|
if(pitch * 4 == Lock.Pitch && !swap_r_b)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-05-29 09:32:44 +00:00
|
|
|
memcpy(Lock.pBits,buffer,Lock.Pitch*height);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2009-12-07 18:48:31 +00:00
|
|
|
else
|
2010-05-29 09:32:44 +00:00
|
|
|
{
|
2009-12-07 18:48:31 +00:00
|
|
|
u32* pIn = pBuffer;
|
2010-05-02 14:05:14 +00:00
|
|
|
if (!swap_r_b) {
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u32 *pBits = (u32*)((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
memcpy(pBits, pIn, width * 4);
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u8 *pIn8 = (u8 *)pIn;
|
|
|
|
u8 *pBits = (u8 *)((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
for (int x = 0; x < width * 4; x += 4) {
|
|
|
|
pBits[x + 0] = pIn8[x + 2];
|
|
|
|
pBits[x + 1] = pIn8[x + 1];
|
|
|
|
pBits[x + 2] = pIn8[x + 0];
|
|
|
|
pBits[x + 3] = pIn8[x + 3];
|
|
|
|
}
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
2009-12-07 18:48:31 +00:00
|
|
|
}
|
2010-05-29 09:32:44 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2008-12-26 17:02:46 +00:00
|
|
|
break;
|
|
|
|
case D3DFMT_DXT1:
|
2010-02-03 14:13:03 +00:00
|
|
|
memcpy(Lock.pBits,buffer,((width+3)/4)*((height+3)/4)*8);
|
2008-12-26 17:02:46 +00:00
|
|
|
break;
|
2009-09-01 19:48:45 +00:00
|
|
|
default:
|
|
|
|
PanicAlert("D3D: Invalid texture format %i", fmt);
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2008-12-26 17:02:46 +00:00
|
|
|
pTexture->UnlockRect(level);
|
|
|
|
return pTexture;
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2009-12-07 18:48:31 +00:00
|
|
|
LPDIRECT3DTEXTURE9 CreateOnlyTexture2D(const int width, const int height, D3DFORMAT fmt)
|
|
|
|
{
|
|
|
|
LPDIRECT3DTEXTURE9 pTexture;
|
|
|
|
// crazy bitmagic, sorry :)
|
|
|
|
bool isPow2 = !((width&(width-1)) || (height&(height-1)));
|
|
|
|
bool bExpand = false;
|
|
|
|
HRESULT hr;
|
|
|
|
// TODO(ector): Allow mipmaps for non-pow textures on newer cards?
|
|
|
|
// TODO(ector): Use the game-specified mipmaps?
|
|
|
|
if (!isPow2)
|
|
|
|
hr = dev->CreateTexture(width, height, 1, 0, fmt, D3DPOOL_MANAGED, &pTexture, NULL);
|
|
|
|
else
|
|
|
|
hr = dev->CreateTexture(width, height, 0, D3DUSAGE_AUTOGENMIPMAP, fmt, D3DPOOL_MANAGED, &pTexture, NULL);
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
return 0;
|
|
|
|
return pTexture;
|
|
|
|
}
|
|
|
|
|
2010-04-14 13:57:16 +00:00
|
|
|
void ReplaceTexture2D(LPDIRECT3DTEXTURE9 pTexture, const u8* buffer, const int width, const int height, const int pitch, D3DFORMAT fmt, bool swap_r_b, int level)
|
2008-12-26 17:02:46 +00:00
|
|
|
{
|
|
|
|
u32* pBuffer = (u32*)buffer;
|
|
|
|
D3DLOCKED_RECT Lock;
|
2009-09-01 19:48:45 +00:00
|
|
|
pTexture->LockRect(level, &Lock, NULL, 0);
|
2008-12-26 17:02:46 +00:00
|
|
|
u32* pIn = pBuffer;
|
2009-09-19 13:14:55 +00:00
|
|
|
|
|
|
|
bool bExpand = false;
|
|
|
|
|
|
|
|
if (fmt == D3DFMT_A8P8) {
|
|
|
|
fmt = D3DFMT_A8L8;
|
|
|
|
bExpand = true;
|
|
|
|
}
|
2010-01-21 19:31:48 +00:00
|
|
|
switch (fmt)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2008-12-26 17:02:46 +00:00
|
|
|
case D3DFMT_A8R8G8B8:
|
2010-05-29 09:32:44 +00:00
|
|
|
if(pitch * 4 == Lock.Pitch && !swap_r_b)
|
|
|
|
{
|
|
|
|
memcpy(Lock.pBits, pBuffer, Lock.Pitch*height);
|
|
|
|
}
|
|
|
|
else if (!swap_r_b)
|
|
|
|
{
|
2008-12-26 17:02:46 +00:00
|
|
|
for (int y = 0; y < height; y++)
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2010-01-21 19:31:48 +00:00
|
|
|
u32 *pBits = (u32*)((u8*)Lock.pBits + (y * Lock.Pitch));
|
2008-12-26 17:02:46 +00:00
|
|
|
memcpy(pBits, pIn, width * 4);
|
|
|
|
pIn += pitch;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2010-05-29 09:32:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-21 19:31:48 +00:00
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u8 *pIn8 = (u8 *)pIn;
|
|
|
|
u8 *pBits = (u8 *)((u8*)Lock.pBits + (y * Lock.Pitch));
|
2010-05-29 09:32:44 +00:00
|
|
|
for (int x = 0; x < width * 4; x += 4)
|
|
|
|
{
|
2010-01-21 19:31:48 +00:00
|
|
|
pBits[x + 0] = pIn8[x + 2];
|
|
|
|
pBits[x + 1] = pIn8[x + 1];
|
|
|
|
pBits[x + 2] = pIn8[x + 0];
|
|
|
|
pBits[x + 3] = pIn8[x + 3];
|
|
|
|
}
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2008-12-26 17:02:46 +00:00
|
|
|
break;
|
2009-09-19 13:14:55 +00:00
|
|
|
case D3DFMT_L8:
|
|
|
|
case D3DFMT_A8:
|
|
|
|
case D3DFMT_A4L4:
|
|
|
|
{
|
|
|
|
const u8 *pIn = buffer;
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u8* pBits = ((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
memcpy(pBits, pIn, width);
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case D3DFMT_R5G6B5:
|
|
|
|
{
|
|
|
|
const u16 *pIn = (u16*)buffer;
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u16* pBits = (u16*)((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
memcpy(pBits, pIn, width * 2);
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case D3DFMT_A8L8:
|
|
|
|
{
|
|
|
|
if (bExpand) { // I8
|
|
|
|
const u8 *pIn = buffer;
|
|
|
|
// TODO(XK): Find a better way that does not involve either unpacking
|
|
|
|
// or downsampling (i.e. A4L4)
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u8* pBits = ((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
for(int i = 0; i < width * 2; i += 2) {
|
|
|
|
pBits[i] = pIn[i / 2];
|
|
|
|
pBits[i + 1] = pIn[i / 2];
|
|
|
|
}
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
|
|
|
} else { // IA8
|
|
|
|
const u16 *pIn = (u16*)buffer;
|
|
|
|
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
u16* pBits = (u16*)((u8*)Lock.pBits + (y * Lock.Pitch));
|
|
|
|
memcpy(pBits, pIn, width * 2);
|
|
|
|
pIn += pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-12-26 17:02:46 +00:00
|
|
|
case D3DFMT_DXT1:
|
2010-02-03 14:13:03 +00:00
|
|
|
memcpy(Lock.pBits,buffer,((width+3)/4)*((height+3)/4)*8);
|
2008-12-26 17:02:46 +00:00
|
|
|
break;
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2008-12-26 17:02:46 +00:00
|
|
|
pTexture->UnlockRect(level);
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2008-12-26 17:02:46 +00:00
|
|
|
} // namespace
|