//------------------------------------------------------------------------------------------------- sdMemoryTexture::sdMemoryTexture(LPDIRECT3DTEXTURE9 spD3DTexture) { NIASSERT(spD3DTexture); // 获取渲染设备 NiDX9Renderer* spRenderer = NiDX9Renderer::GetRenderer(); NIASSERT(spRenderer); // 提取格式信息 D3DSURFACE_DESC kLevelDesc; spD3DTexture->GetLevelDesc(0, &kLevelDesc); m_uiWidth = kLevelDesc.Width; m_uiHeight = kLevelDesc.Height; m_uiLevel = spD3DTexture->GetLevelCount(); m_eFormat = kLevelDesc.Format; // m_spD3DTexture = spD3DTexture; m_spD3DTexture->AddRef(); // 创建GB纹理对象 m_spTexture = spRenderer->CreateNiTextureFromD3DTexture(m_spD3DTexture); NIASSERT(m_spTexture); }
void SetD3DResourcePrivateData(LPDIRECT3DRESOURCE9 res, const char* FName) { #if R3D_SET_DEBUG_D3D_NAMES DWORD sz = strlen(FName); res->SetPrivateData(WKPDID_D3DDebugObjectName, FName, sz, 0); void* p = 0; res->QueryInterface(IID_IDirect3DTexture9, &p); if(p) { LPDIRECT3DTEXTURE9 t = (LPDIRECT3DTEXTURE9)p; int mipsCount = t->GetLevelCount(); for (int i = 0; i < mipsCount; ++i) { LPDIRECT3DSURFACE9 surf; t->GetSurfaceLevel(i, &surf); surf->SetPrivateData(WKPDID_D3DDebugObjectName, FName, sz, 0); surf->Release(); } t->Release(); return; } p = 0; res->QueryInterface(IID_IDirect3DCubeTexture9, &p); if(p) { LPDIRECT3DCUBETEXTURE9 t = (LPDIRECT3DCUBETEXTURE9)p; int mipsCount = t->GetLevelCount(); for (int i = 0; i < mipsCount; ++i) { for (int j = D3DCUBEMAP_FACE_POSITIVE_X; j <= D3DCUBEMAP_FACE_NEGATIVE_Z; ++j) { LPDIRECT3DSURFACE9 surf; t->GetCubeMapSurface((D3DCUBEMAP_FACES)j, i, &surf); surf->SetPrivateData(WKPDID_D3DDebugObjectName, FName, sz, 0); surf->Release(); } } t->Release(); return; } #endif }
void AmjuGLDX9::SetTexture( AmjuGL::TextureHandle* th, AmjuGL::TextureType tt, AmjuGL::TextureDepth td, int width, int height, uint8* data) { AMJU_CALL_STACK; HRESULT res = D3DXCreateTexture( dd, //LPDIRECT3DDEVICE9 pDevice, width, //UINT Width, height, //UINT Height, 0, // Mip Levels: 0 means create all D3DUSAGE_DYNAMIC, //DWORD Usage, (td == AmjuGL::AMJU_RGB ? D3DFMT_R8G8B8 : D3DFMT_A8R8G8B8), //D3DFORMAT Format, D3DPOOL_DEFAULT, // Pool, reinterpret_cast<LPDIRECT3DTEXTURE9*>(th) //LPDIRECT3DTEXTURE9 * ppTexture ); LPDIRECT3DTEXTURE9 pTex = reinterpret_cast<LPDIRECT3DTEXTURE9>(*th); D3DLOCKED_RECT lockedRect; pTex->LockRect(0, &lockedRect, NULL, 0); switch (td) { case AmjuGL::AMJU_RGB: CopyRGBTexture((uint8*)lockedRect.pBits, lockedRect.Pitch, data, width, height); break; case AmjuGL::AMJU_RGBA: CopyRGBATexture((uint8*)lockedRect.pBits, lockedRect.Pitch, data, width, height); break; } pTex->UnlockRect(0); // TODO Create data for each mipmap level. // NB We want the same functionality as screenshot shrinking. IDirect3DSurface9 * pSurfaceLevel; for (unsigned int iLevel = 0; iLevel < pTex->GetLevelCount(); iLevel++) { pTex->GetSurfaceLevel(iLevel, &pSurfaceLevel); // TODO Write this mip map pSurfaceLevel->Release(); } }
//-------------------------------------------------------------------------------------- // Name: BuildMipMaps() // Desc: Generate mip maps from the base texture //-------------------------------------------------------------------------------------- VOID PostProcess::BuildMipMaps( LPDIRECT3DTEXTURE9 pTexture ) { // Make sure that the required shaders and objects exist assert( m_pCopyTexturePS ); assert( pTexture ); DWORD dwNumMipLevels = pTexture->GetLevelCount(); // Create and set a render target PushRenderTarget( 0L, CreateRenderTarget( pTexture ) ); // Scale and copy the src texture g_pd3dDevice->SetPixelShader( m_pCopyTexturePS ); g_pd3dDevice->SetTexture( 0, pTexture ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); D3DVIEWPORT9 vp; g_pd3dDevice->GetViewport( &vp ); for( DWORD i = 1; i < dwNumMipLevels; i++ ) { XGTEXTURE_DESC Desc; XGGetTextureDesc( pTexture, i, &Desc ); vp.Width = Desc.Width; vp.Height = Desc.Height; g_pd3dDevice->SetViewport( &vp ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINMIPLEVEL, i - 1 ); // Draw a fullscreen quad to sample the RT DrawFullScreenQuad(); DWORD ColorExpBias = 0; if( Desc.Format == D3DFMT_G16R16_SIGNED_INTEGER ) ColorExpBias = (DWORD) D3DRESOLVE_EXPONENTBIAS(10); else if( Desc.Format == D3DFMT_A16B16G16R16_SIGNED_INTEGER ) ColorExpBias = (DWORD) D3DRESOLVE_EXPONENTBIAS(10); g_pd3dDevice->Resolve( D3DRESOLVE_RENDERTARGET0 | ColorExpBias, NULL, pTexture, NULL, i, 0, NULL, 1.0f, 0L, NULL ); } // Cleanup and return g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINMIPLEVEL, 13 ); PopRenderTarget( 0L )->Release(); g_pd3dDevice->SetPixelShader( NULL ); }
void cTexture::BltToTextureSurface(LPDIRECT3DTEXTURE9 pTempTex ) { SafeRelease( m_pTexture ); D3DSURFACE_DESC TexDesc; pTempTex->GetLevelDesc( 0, &TexDesc ); DWORD NumLevels = pTempTex->GetLevelCount(); D3DXCreateTexture( Graphics()->GetDevice(), TexDesc.Width, TexDesc.Height, NumLevels, 0, TexDesc.Format, D3DPOOL_MANAGED, &m_pTexture ); LPDIRECT3DSURFACE9 pSrcSurf = 0; LPDIRECT3DSURFACE9 pDestSurf = 0; for( int i = 0 ; i < NumLevels ; i++ ) { m_pTexture->GetSurfaceLevel( i, &pDestSurf ); pTempTex->GetSurfaceLevel( i, &pSrcSurf ); D3DXLoadSurfaceFromSurface( pDestSurf, 0, 0, pSrcSurf, 0, 0, D3DX_FILTER_NONE, 0 ); pDestSurf->Release(); pSrcSurf->Release(); } }
void WaterEffect::UpdateTexture( Real FrameTime, Screen3D& Screen ) { //Disperse the water /*for(int y=1; y<WATERDIM-1; y++) { for(int x=1; x<WATERDIM-1; x++) { if(NewWater[x+y*WATERDIM] == -1) continue; NewWater[x+y * WATERDIM] = ((OldWater[x+ ((y-1)*WATERDIM)] + OldWater[x+ ((y+1)*WATERDIM)] + OldWater[(x+1)+ (y*WATERDIM)] + OldWater[(x-1)+ (y*WATERDIM)] )*.5f) - NewWater[x+y * WATERDIM]; NewWater[x+y * WATERDIM] -= (NewWater[x+y * WATERDIM]*0.0625) ; if(NewWater[x+y * WATERDIM] < 0) NewWater[x+y * WATERDIM] =0; if(NewWater[x+y * WATERDIM] > 250) NewWater[x+y * WATERDIM] = 250; } }*/ for(int i=0; i<GRID_DIM*GRID_DIM; i++) { if(JumpTable[i]) { for(int y=JumpTable[i]->StartY; y<JumpTable[i]->EndY; y++) { for(int x=JumpTable[i]->StartX; x<JumpTable[i]->EndX; x++) { if(NewWater[x+y*WATERDIM] == -1) continue; NewWater[x+y * WATERDIM] = ((OldWater[x+ ((y-1)*WATERDIM)] + OldWater[x+ ((y+1)*WATERDIM)] + OldWater[(x+1)+ (y*WATERDIM)] + OldWater[(x-1)+ (y*WATERDIM)] )*.5f) - NewWater[x+y * WATERDIM]; NewWater[x+y * WATERDIM] -= (NewWater[x+y * WATERDIM]*0.0625) * FrameTime; if(NewWater[x+y * WATERDIM] < 0) NewWater[x+y * WATERDIM] =0; if(NewWater[x+y * WATERDIM] > 250) NewWater[x+y * WATERDIM] = 250; } } } } //Update the texture LPDIRECT3DTEXTURE9 TextureSurface; D3DSURFACE_DESC SurfDesc; D3DLOCKED_RECT LockedArea; BYTE* Surface; Texture* T = Screen.TM.GetTexture(RippleTexture); TextureSurface = T->GetTextureSurface(); //Figure out how many "levels" (mip-maps) there are //in the texture int nLevels = TextureSurface->GetLevelCount(); TextureSurface->GetLevelDesc(0, &SurfDesc); Real DivFactor=1; //loop through each level for(int l=0; l<nLevels; l++) { //Get the surface's attributes TextureSurface->GetLevelDesc(l, &SurfDesc); //lock down a pointer TextureSurface->LockRect(l, &LockedArea, NULL, NULL); //draw the pixel to the surface Surface = (BYTE*)LockedArea.pBits; if(SurfDesc.Format == D3DFMT_A8R8G8B8) { for(int y=0; y<WATERDIM/DivFactor; y++) { for(int x=0; x<WATERDIM/DivFactor; x++) { int rx=x*DivFactor; int ry=y*DivFactor; Surface[(x*4)+0+y*(SurfDesc.Width*4)] = 128+NewWater[rx+ry * WATERDIM]; Surface[(x*4)+1+y*(SurfDesc.Width*4)] = 128+NewWater[rx+ry * WATERDIM]; Surface[(x*4)+2+y*(SurfDesc.Width*4)] = 128+NewWater[rx+ry * WATERDIM]; } } } if(SurfDesc.Format == D3DFMT_L8) { for(int y=0; y<WATERDIM/DivFactor; y++) { for(int x=0; x<WATERDIM/DivFactor; x++) { int rx=x*DivFactor; int ry=y*DivFactor; Surface[(x+y*SurfDesc.Width)] = 128+NewWater[rx+ry * WATERDIM]; } } } DivFactor *=2; //unlock the surface TextureSurface->UnlockRect(l); } //Swap the buffers Real* Temp = NewWater; NewWater = OldWater; OldWater = Temp; }
/* Draws primitives on screen */ void D3DRenderer::OnDrawBatch(const Otter::DrawBatch& batch) { mModel = *(D3DXMATRIX*)&batch.mTransform; LPDIRECT3DTEXTURE9 texture = mTextureManager.GetTexture(batch.mTextureID); const Otter::Property* pSetShader = batch.mProperties.GetProperty(SHADER_SET); if(pSetShader) { uint32 newShader = (uint32)pSetShader->mData; SetShader(newShader, false); } Shader& shader = mShaders[mCurrentShaderID]; const Otter::Property* pShaderParamCount = batch.mProperties.GetProperty(SHADER_PARAM_COUNT); if(pShaderParamCount) { uint32 cnt = (uint32)pShaderParamCount->mData; for(uint32 i = 0; i < cnt; i++) { const Otter::Property* pParamType = batch.mProperties.GetProperty(SHADER_PARAM_TYPE + i); const Otter::Property* pParamID = batch.mProperties.GetProperty(SHADER_PARAM_ID + i); const Otter::Property* pParamData = batch.mProperties.GetProperty(SHADER_PARAM_DATA + i); const Otter::Property* pParamDataLen = batch.mProperties.GetProperty(SHADER_PARAM_DATA_LEN + i); if(pParamType == NULL || pParamID == NULL || pParamData == NULL) break; if(pParamType->mData == 0) // Texture { shader.mEffect->SetTexture(shader.GetParameterHandle((int)pParamID->mData), mTextureManager.GetTexture(pParamData->mData)); } else if(pParamType->mData == 1 && pParamDataLen != NULL) // Float Array { shader.mEffect->SetFloatArray(shader.GetParameterHandle((int)pParamID->mData), (float*)pParamData->mData, (int)pParamDataLen->mData); } else if(pParamType->mData == 2 && pParamDataLen != NULL) // Int Array { shader.mEffect->SetIntArray(shader.GetParameterHandle((int)pParamID->mData), (int*)pParamData->mData, (int)pParamDataLen->mData); } else if(pParamType->mData == 3) // Int { shader.mEffect->SetInt(shader.GetParameterHandle((int)pParamID->mData), (int)pParamData->mData); } } } const Otter::Property* pTechnique = batch.mProperties.GetProperty(SHADER_TECHNIQUE); if(pTechnique) shader.mEffect->SetTechnique((const char*)pTechnique->mData); else shader.mEffect->SetTechnique(texture != NULL ? "WithTexture" : "WithoutTexture"); shader.mEffect->SetTexture("DIFFUSE_TEXTURE", texture); shader.mEffect->SetMatrix("MATRIX_MODEL", &mModel); float lodBias = -1.0f; if(texture != NULL) { int numLevels = texture->GetLevelCount(); mD3DDevice->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, *(DWORD*)&lodBias); } if(batch.mRenderFlags & Otter::kRender_Wireframe) { mD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); } else { mD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); } uint32 passes = 0; shader.mEffect->Begin(&passes, D3DXFX_DONOTSAVESAMPLERSTATE); { for(uint32 pass = 0; pass < passes; pass++) { shader.mEffect->BeginPass(pass); D3DPRIMITIVETYPE primType = D3DPT_TRIANGLELIST; switch(batch.mPrimitiveType) { case Otter::kPrim_TriangleFan: { primType = D3DPT_TRIANGLEFAN; break; } case Otter::kPrim_TriangleStrip: { primType = D3DPT_TRIANGLESTRIP; break; } } mD3DDevice->DrawPrimitive(primType, batch.mVertexStartIndex, batch.mPrimitiveCount); shader.mEffect->EndPass(); } } shader.mEffect->End(); }