static bool grabFrameD3D10(IDXGISwapChain *swap) { ID3D10Device *device = 0; ID3D10Texture2D *tex = 0, *captureTex = 0; if (FAILED(swap->GetBuffer(0, IID_ID3D10Texture2D, (void**)&tex))) return false; D3D10_TEXTURE2D_DESC desc; tex->GetDevice(&device); tex->GetDesc(&desc); // re-creating the capture staging texture each frame is definitely not the most efficient // way to handle things, but it frees me of all kind of resource management trouble, so // here goes... desc.MipLevels = 1; desc.ArraySize = 1; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Usage = D3D10_USAGE_STAGING; desc.BindFlags = 0; desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; desc.MiscFlags = 0; if(FAILED(device->CreateTexture2D(&desc,0,&captureTex))) printLog("video/d3d10: couldn't create staging texture for gpu->cpu download!\n"); else setCaptureResolution(desc.Width,desc.Height); if(device) device->CopySubresourceRegion(captureTex,0,0,0,0,tex,0,0); D3D10_MAPPED_TEXTURE2D mapped; bool grabOk = false; if(captureTex && SUCCEEDED(captureTex->Map(0,D3D10_MAP_READ,0,&mapped))) { switch(desc.Format) { case DXGI_FORMAT_R8G8B8A8_UNORM: blitAndFlipRGBAToCaptureData((unsigned char *) mapped.pData,mapped.RowPitch); grabOk = true; break; default: printLog("video/d3d10: unsupported backbuffer format, can't grab pixels!\n"); break; } captureTex->Unmap(0); } tex->Release(); if(captureTex) captureTex->Release(); if(device) device->Release(); return grabOk; }
void TextureHandler::addTexture(std::string fileName, std::string textureName) { ID3D10ShaderResourceView * rv; ID3D10Texture2D * GPUTexture; ID3D10Texture2D * CPUTexture; HRESULT hr = D3DX10CreateTextureFromFile(mpDevice, fileName.c_str(), NULL, NULL, (ID3D10Resource**)&GPUTexture, NULL ); if(FAILED(hr)) { DEBUG_MESSAGE("Failed to create GPU texture from file: "<<fileName); return; } D3DX10_IMAGE_LOAD_INFO loadInfo; ZeroMemory(&loadInfo,sizeof(D3DX10_IMAGE_LOAD_INFO)); loadInfo.Width = D3DX10_DEFAULT; loadInfo.Height = D3DX10_DEFAULT; loadInfo.BindFlags = 0; loadInfo.Depth = D3DX10_DEFAULT; loadInfo.Filter = D3DX10_DEFAULT; loadInfo.FirstMipLevel = D3DX10_DEFAULT; loadInfo.Format = DXGI_FORMAT_FROM_FILE; loadInfo.MipFilter = D3DX10_DEFAULT; loadInfo.MiscFlags = D3DX10_DEFAULT; loadInfo.pSrcInfo = 0; loadInfo.Usage = D3D10_USAGE_STAGING; loadInfo.CpuAccessFlags = D3D10_CPU_ACCESS_READ; hr = D3DX10CreateTextureFromFile(mpDevice, fileName.c_str(), &loadInfo, NULL, (ID3D10Resource**)&CPUTexture, NULL ); if(FAILED(hr)) { DEBUG_MESSAGE("Failed to create CPU texture from file: "<<fileName); return; } D3D10_TEXTURE2D_DESC texDesc; GPUTexture->GetDesc(&texDesc); D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; viewDesc.Format = texDesc.Format; viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; viewDesc.Texture2D.MipLevels = 1; viewDesc.Texture2D.MostDetailedMip = 0; hr = mpDevice->CreateShaderResourceView(GPUTexture,&viewDesc,&rv); GPUTexture->Release(); if(FAILED(hr)) { DEBUG_MESSAGE("Failed to create GPU resource View. Filename: "<<fileName); return; } mTextures.push_back(myNew Texture(rv,CPUTexture,textureName)); }
HRESULT CheckRenderD3D10::ResourceToPPM(ID3D10Device *pDevice, ID3D10Resource *pResource, const char *zFileName) { D3D10_RESOURCE_DIMENSION rType; pResource->GetType(&rType); if (rType != D3D10_RESOURCE_DIMENSION_TEXTURE2D) { printf("SurfaceToPPM: pResource is not a 2D texture! Aborting...\n"); return E_FAIL; } ID3D10Texture2D *pSourceTexture = (ID3D10Texture2D *)pResource; ID3D10Texture2D *pTargetTexture = NULL; D3D10_TEXTURE2D_DESC desc; pSourceTexture->GetDesc(&desc); desc.BindFlags = 0; desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; desc.Usage = D3D10_USAGE_STAGING; if (FAILED(pDevice->CreateTexture2D(&desc,NULL,&pTargetTexture))) { printf("SurfaceToPPM: Unable to create target Texture resoruce! Aborting... \n"); return E_FAIL; } pDevice->CopyResource(pTargetTexture,pSourceTexture); D3D10_MAPPED_TEXTURE2D mappedTex2D; pTargetTexture->Map(0,D3D10_MAP_READ,0,&mappedTex2D); // Need to convert from dx pitch to pitch=width unsigned char *pPPMData = new unsigned char[desc.Width*desc.Height*4]; for (unsigned int iHeight = 0; iHeight<desc.Height; iHeight++) { memcpy(&(pPPMData[iHeight*desc.Width*4]),(unsigned char *)(mappedTex2D.pData)+iHeight*mappedTex2D.RowPitch,desc.Width*4); } pTargetTexture->Unmap(0); // Prepends the PPM header info and bumps byte data afterwards sdkSavePPM4ub(zFileName, pPPMData, desc.Width, desc.Height); delete [] pPPMData; pTargetTexture->Release(); return S_OK; }
void CRenderTarget::u_setrt (const ref_rt& _1, const ref_rt& _2, const ref_rt& _3, ID3DDepthStencilView* zb) { VERIFY (_1||zb); if (_1) { dwWidth = _1->dwWidth; dwHeight = _1->dwHeight; } else { D3D10_DEPTH_STENCIL_VIEW_DESC desc; zb->GetDesc(&desc); if( !RImplementation.o.dx10_msaa ) VERIFY(desc.ViewDimension==D3D10_DSV_DIMENSION_TEXTURE2D); ID3D10Resource *pRes; zb->GetResource( &pRes); ID3D10Texture2D *pTex = (ID3D10Texture2D *)pRes; D3D10_TEXTURE2D_DESC TexDesc; pTex->GetDesc(&TexDesc); dwWidth = TexDesc.Width; dwHeight = TexDesc.Height; _RELEASE( pRes ); } if (_1) RCache.set_RT(_1->pRT, 0); else RCache.set_RT(NULL,0); if (_2) RCache.set_RT(_2->pRT, 1); else RCache.set_RT(NULL,1); if (_3) RCache.set_RT(_3->pRT, 2); else RCache.set_RT(NULL,2); RCache.set_ZB (zb); // RImplementation.rmNormal (); }
SCTReturn SCTTextureD3D10::Initialize(const WCHAR *filename, bool asCubeMap) { // Load the texture HRESULT result; if(asCubeMap) { D3DX10_IMAGE_LOAD_INFO loadInfo; loadInfo.MiscFlags = D3D10_RESOURCE_MISC_TEXTURECUBE; ID3D10Texture2D* tex = 0; result = D3DX10CreateTextureFromFile(mpDevice, filename, &loadInfo, 0, (ID3D10Resource**)&tex, 0); D3D10_TEXTURE2D_DESC texDesc; tex->GetDesc(&texDesc); D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; viewDesc.Format = texDesc.Format; viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE; viewDesc.TextureCube.MipLevels = texDesc.MipLevels; viewDesc.TextureCube.MostDetailedMip = 0; result = mpDevice->CreateShaderResourceView(tex, &viewDesc, &mpShaderResourceView); ReleaseCOM(tex); } else { result = D3DX10CreateShaderResourceViewFromFile(mpDevice, filename, NULL, NULL, &mpShaderResourceView, NULL); } if(result != S_OK) return FAIL; return OK; }
ID3D10ShaderResourceView * TextureHandler::getTextureArray(std::string textureNames) const { int nrOfTextures = 1; for(unsigned int i=0;i<textureNames.size();i++) { if(textureNames[i]==',') { nrOfTextures++; } } ID3D10Texture2D *GPUTexArray; D3D10_TEXTURE2D_DESC arrayDesc; for(int i=0;i<nrOfTextures;i++) { int pos = textureNames.find_first_of(","); string name; if(pos==string::npos) { name = textureNames; textureNames.clear(); } else { name = textureNames.substr(0,pos); textureNames.erase(0,pos+1); } bool textureFound = false; for(unsigned int j=0;j<mTextures.size();j++) { if(name==mTextures[j]->getName()) { ID3D10Texture2D * texture; texture = mTextures[j]->getCPUTexture(); if(i==0) { D3D10_TEXTURE2D_DESC firstTexDesc; texture->GetDesc(&firstTexDesc); ZeroMemory(&arrayDesc, sizeof(D3D10_TEXTURE2D_DESC)); arrayDesc.Width = firstTexDesc.Width; arrayDesc.Height = firstTexDesc.Height; arrayDesc.MipLevels = 1; arrayDesc.Format = firstTexDesc.Format; arrayDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE; arrayDesc.Usage = D3D10_USAGE_DEFAULT; arrayDesc.ArraySize = nrOfTextures; arrayDesc.SampleDesc = firstTexDesc.SampleDesc; arrayDesc.CPUAccessFlags = 0; arrayDesc.MiscFlags = 0; if(FAILED(mpDevice->CreateTexture2D(&arrayDesc, NULL, &GPUTexArray))) { DEBUG_MESSAGE("Failed to create GPUTexArray."); return NULL; } } else { D3D10_TEXTURE2D_DESC texDesc; texture->GetDesc(&texDesc); if(texDesc.Width!=arrayDesc.Width) { DEBUG_MESSAGE("Trying to create a texture array from textures with different width."); return NULL; } else if(texDesc.Height!=arrayDesc.Height) { DEBUG_MESSAGE("Trying to create a texture array from textures with different height."); return NULL; } } D3D10_MAPPED_TEXTURE2D texMap; texture->Map(0,D3D10_MAP_READ,0,&texMap); unsigned int SRIndex = D3D10CalcSubresource(0,i,1); mpDevice->UpdateSubresource(GPUTexArray, SRIndex, NULL, texMap.pData, texMap.RowPitch, 1); texture->Unmap(0); textureFound = true; break; } } if(!textureFound) { DEBUG_MESSAGE("Could not find a texture with the name: "<<name); return NULL; } } D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc; ZeroMemory(&SRVDesc, sizeof(D3D10_SHADER_RESOURCE_VIEW_DESC)); SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2DARRAY; SRVDesc.Format = arrayDesc.Format; SRVDesc.Texture2DArray.ArraySize = nrOfTextures; SRVDesc.Texture2DArray.FirstArraySlice = 0; SRVDesc.Texture2DArray.MipLevels = 1; SRVDesc.Texture2DArray.MostDetailedMip = 0; ID3D10ShaderResourceView *texArraySRV = NULL; if(FAILED(mpDevice->CreateShaderResourceView(GPUTexArray, &SRVDesc, &texArraySRV))) { DEBUG_MESSAGE("Failed to create Tex2DArray ShaderResourceView."); return NULL; } return texArraySRV; }
// Detour function that replaces the IDXGISwapChain::Present() API DllExport HRESULT __stdcall hook_DXGISwapChainPresent( IDXGISwapChain * This, UINT SyncInterval, UINT Flags ) { static int frame_interval; static LARGE_INTEGER initialTv, captureTv, freq; static int capture_initialized = 0; // int i; struct pooldata *data; struct vsource_frame *frame; // DXGI_SWAP_CHAIN_DESC pDESC; HRESULT hr = pDXGISwapChainPresent(This, SyncInterval, Flags); if(resolution_retrieved == 0) { if(DXGI_get_resolution(This) >= 0) { resolution_retrieved = 1; } return hr; } if(vsource_initialized == 0) { ga_error("video source not initialized.\n"); return hr; } This->GetDesc(&pDESC); pDXGI_FORMAT = pDESC.BufferDesc.Format; // extract screen format for sws_scale if(pDESC.BufferDesc.Width != game_width || pDESC.BufferDesc.Height != game_height) { ga_error("game width/height mismatched (%dx%d) != (%dx%d)\n", pDESC.BufferDesc.Width, pDESC.BufferDesc.Height, game_width, game_height); return hr; } // if (enable_server_rate_control && ga_hook_video_rate_control() < 0) return hr; if (dx_version == dx_none) { //bool check_result = FALSE; if (check_dx_device_version(This, IID_ID3D10Device)) { dx_version = dx_10; ga_error("[DXGISwapChain] DirectX 10\n"); } else if (check_dx_device_version(This, IID_ID3D10Device1)) { dx_version = dx_10_1; ga_error("[DXGISwapChain] DirectX 10.1\n"); } else if (check_dx_device_version(This, IID_ID3D11Device)) { dx_version = dx_11; ga_error("[DXGISwapChain] DirectX 11\n"); } } if (capture_initialized == 0) { frame_interval = 1000000/video_fps; // in the unif of us frame_interval++; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&initialTv); capture_initialized = 1; } else { QueryPerformanceCounter(&captureTv); } hr = 0; // d3d10 / d3d10.1 if (dx_version == dx_10 || dx_version == dx_10_1) { void *ppDevice; ID3D10Device *pDevice; //IUnknown *pDevice; if (dx_version == dx_10) { This->GetDevice(IID_ID3D10Device, &ppDevice); pDevice = (ID3D10Device *)ppDevice; } else if (dx_version == dx_10_1) { This->GetDevice(IID_ID3D10Device1, &ppDevice); pDevice = (ID3D10Device1 *)ppDevice; } else { OutputDebugString("Invalid DirectX version in IDXGISwapChain::Present"); return hr; } ID3D10RenderTargetView *pRTV = NULL; ID3D10Resource *pSrcResource = NULL; pDevice->OMGetRenderTargets(1, &pRTV, NULL); pRTV->GetResource(&pSrcResource); ID3D10Texture2D* pSrcBuffer = (ID3D10Texture2D *)pSrcResource; ID3D10Texture2D* pDstBuffer = NULL; D3D10_TEXTURE2D_DESC desc; pSrcBuffer->GetDesc(&desc); desc.BindFlags = 0; desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; desc.Usage = D3D10_USAGE_STAGING; hr = pDevice->CreateTexture2D(&desc, NULL, &pDstBuffer); if (FAILED(hr)) { OutputDebugString("Failed to create texture2D"); //assert(exp_state == exp_none); } pDevice->CopyResource(pDstBuffer, pSrcBuffer); D3D10_MAPPED_TEXTURE2D mapped_screen; hr = pDstBuffer->Map(0, D3D10_MAP_READ, 0, &mapped_screen); if (FAILED(hr)) { OutputDebugString("Failed to map from DstBuffer"); //assert(exp_state == exp_none); } // copy image do { unsigned char *src, *dst; data = g_pipe[0]->allocate_data(); frame = (struct vsource_frame*) data->ptr; frame->pixelformat = PIX_FMT_BGRA; frame->realwidth = desc.Width; frame->realheight = desc.Height; frame->realstride = desc.Width<<2; frame->realsize = frame->realwidth * frame->realstride; frame->linesize[0] = frame->realstride;//frame->stride; // src = (unsigned char*) mapped_screen.pData; dst = (unsigned char*) frame->imgbuf; for (i = 0; i < encoder_height; i++) { CopyMemory(dst, src, frame->realstride/*frame->stride*/); src += mapped_screen.RowPitch; dst += frame->realstride;//frame->stride; } frame->imgpts = pcdiff_us(captureTv, initialTv, freq)/frame_interval; } while(0); // duplicate from channel 0 to other channels for(i = 1; i < SOURCES; i++) { int j; struct pooldata *dupdata; struct vsource_frame *dupframe; dupdata = g_pipe[i]->allocate_data(); dupframe = (struct vsource_frame*) dupdata->ptr; // vsource_dup_frame(frame, dupframe); // g_pipe[i]->store_data(dupdata); g_pipe[i]->notify_all(); } g_pipe[0]->store_data(data); g_pipe[0]->notify_all(); pDstBuffer->Unmap(0); pDevice->Release(); pSrcResource->Release(); pSrcBuffer->Release(); pRTV->Release(); pDstBuffer->Release(); // d11 } else if (dx_version == dx_11) { void *ppDevice; This->GetDevice(IID_ID3D11Device, &ppDevice); ID3D11Device *pDevice = (ID3D11Device*) ppDevice; This->GetDevice(IID_ID3D11DeviceContext, &ppDevice); ID3D11DeviceContext *pDeviceContext = (ID3D11DeviceContext *) ppDevice; ID3D11RenderTargetView *pRTV = NULL; ID3D11Resource *pSrcResource = NULL; pDeviceContext->OMGetRenderTargets(1, &pRTV, NULL); pRTV->GetResource(&pSrcResource); ID3D11Texture2D *pSrcBuffer = (ID3D11Texture2D *)pSrcResource; ID3D11Texture2D *pDstBuffer = NULL; D3D11_TEXTURE2D_DESC desc; pSrcBuffer->GetDesc(&desc); desc.BindFlags = 0; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.Usage = D3D11_USAGE_STAGING; hr = pDevice->CreateTexture2D(&desc, NULL, &pDstBuffer); if (FAILED(hr)) { OutputDebugString("Failed to create buffer"); //assert(exp_state == exp_none); } pDeviceContext->CopyResource(pDstBuffer, pSrcBuffer); D3D11_MAPPED_SUBRESOURCE mapped_screen; hr = pDeviceContext->Map(pDstBuffer, 0, D3D11_MAP_READ, 0, &mapped_screen); if (FAILED(hr)) { OutputDebugString("Failed to map from DeviceContext"); //assert(exp_state == exp_none); } // copy image do { unsigned char *src, *dst; data = g_pipe[0]->allocate_data(); frame = (struct vsource_frame*) data->ptr; frame->pixelformat = PIX_FMT_BGRA; frame->realwidth = desc.Width; frame->realheight = desc.Height; frame->realstride = desc.Width<<2; frame->realsize = frame->realwidth * frame->realstride; frame->linesize[0] = frame->realstride;//frame->stride; // src = (unsigned char*) mapped_screen.pData; dst = (unsigned char*) frame->imgbuf; for (i = 0; i < encoder_height; i++) { CopyMemory(dst, src, frame->realstride/*frame->stride*/); src += mapped_screen.RowPitch; dst += frame->realstride;//frame->stride; } frame->imgpts = pcdiff_us(captureTv, initialTv, freq)/frame_interval; } while(0); // duplicate from channel 0 to other channels for(i = 1; i < SOURCES; i++) { int j; struct pooldata *dupdata; struct vsource_frame *dupframe; dupdata = g_pipe[i]->allocate_data(); dupframe = (struct vsource_frame*) dupdata->ptr; // vsource_dup_frame(frame, dupframe); // g_pipe[i]->store_data(dupdata); g_pipe[i]->notify_all(); } g_pipe[0]->store_data(data); g_pipe[0]->notify_all(); pDeviceContext->Unmap(pDstBuffer, 0); pDevice->Release(); pDeviceContext->Release(); pSrcResource->Release(); pSrcBuffer->Release(); pRTV->Release(); pDstBuffer->Release(); } return hr; }
void ColoredCubeApp::initApp() { D3DApp::initApp(); myOutFile.open("debug.txt"); fx::InitAll(md3dDevice); mBox.init(md3dDevice, 1.0f); mPlane.init(md3dDevice, 1.0f); //we send scale of 1.0f what does that mean?? mTree.initObject(md3dDevice); std::string treeFileName = "MediumPolyTree.3ds"; mTree.load(md3dDevice, treeFileName); mTree.createTexturesAll(L"LeafCol.jpg", L"LeafAlpha.jpg", L"mySpec.jpg", L"LeafnormalX_normals.PNG"); mTree.setCubeMap(Object::createCubeTex(md3dDevice, L"grassenvmap1024.dds")); mTree.rotate(-1.5f,0.0f,0.0f); mTree.translate(3.0f,-3.0f,-2.6f); mObjBox.initObject(md3dDevice); std::string boxFileName = "man2.fbx"; mObjBox.load(md3dDevice, boxFileName); mObjBox.createTexturesAll( L"manD.jpg", L"defaultAlpha.jpg", L"defaultspec.dds", L"manN.jpg"); mObjBox.createTexturesAt(0, L"bricks.dds", L"defaultAlpha.jpg", L"spec.dds", L"womanHairN.jpg"); mObjBox.setCubeMap(Object::createCubeTex(md3dDevice, L"grassenvmap1024.dds")); mObjBox.rotate(0.0f,0.0f,3.14f); mObjBox.translate(-3.0f,0.0f,2.5f); mObjBox.scale(2.0f,2.0f,2.0f); buildFX(); buildVertexLayouts(); //mLights[0].dir = D3DXVECTOR3(0.57735f, -0.57735f, 0.57735f); mLights[0].dir = D3DXVECTOR3(0.576f, -0.576f, -0.576f); mLights[0].ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); mLights[0].diffuse = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); mLights[0].specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); // Pointlight--position is changed every frame to animate. mLights[1].pos = D3DXVECTOR3(2.0f,2.0f,2.0f); mLights[1].ambient = D3DXCOLOR(0.8f, 0.8f, 0.0f, 1.0f); mLights[1].diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); mLights[1].specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); mLights[1].att.x = 0.0f; mLights[1].att.y = 1.0f; mLights[1].att.z = 0.0f; mLights[1].range = 120.0f; animationTimeElapsed = 0.0f; animationTimePrev = mTimer.getGameTime(); for(int i = 0; i < fireFrameCount; i++) { std::wostringstream fileName; fileName << L"FireAnim\\Fire"; if(i+1 < 10) fileName << L"00"; else if(i+1 < 100) fileName << L"0"; fileName << i+1 << L".bmp"; std::wstring wbuffer = fileName.str(); LPCWSTR usableName = wbuffer.c_str(); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, usableName, 0, 0, &mFireAnimationMapRVs[i], 0)); } HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"bricks.dds", 0, 0, &mCrateMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"bricks_normal.dds", 0, 0, &mDefaultNormalMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"stone_diffuse.dds", 0, 0, &mGrassMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"stone_normal.dds", 0, 0, &mBrickNormalMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"spec.dds", 0, 0, &mSpecularMapRV, 0 )); // If not, create it. D3DX10_IMAGE_LOAD_INFO loadInfo; loadInfo.MiscFlags = D3D10_RESOURCE_MISC_TEXTURECUBE; ID3D10Texture2D* tex = 0; HR(D3DX10CreateTextureFromFile(md3dDevice, L"grassenvmap1024.dds", &loadInfo, 0, (ID3D10Resource**)&tex, 0) ); D3D10_TEXTURE2D_DESC texDesc; tex->GetDesc(&texDesc); D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; viewDesc.Format = texDesc.Format; viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE; viewDesc.TextureCube.MipLevels = texDesc.MipLevels; viewDesc.TextureCube.MostDetailedMip = 0; HR(md3dDevice->CreateShaderResourceView(tex, &viewDesc, &mCubeMapRV)); ReleaseCOM(tex); mSky.init(md3dDevice, mCubeMapRV, 5000.0f); }
//-------------------------------------------------------------------------------------- // Create Direct3D device and swap chain //-------------------------------------------------------------------------------------- HRESULT InitDevice() { HRESULT hr = S_OK; RECT rc; GetClientRect( g_hWnd, &rc ); UINT width = rc.right - rc.left; UINT height = rc.bottom - rc.top; UINT createDeviceFlags = 0; #ifdef _DEBUG createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG; #endif D3D10_DRIVER_TYPE driverTypes[] = { D3D10_DRIVER_TYPE_HARDWARE, D3D10_DRIVER_TYPE_REFERENCE, }; UINT numDriverTypes = sizeof( driverTypes ) / sizeof( driverTypes[0] ); DXGI_SWAP_CHAIN_DESC sd; ZeroMemory( &sd, sizeof( sd ) ); sd.BufferCount = 1; sd.BufferDesc.Width = width; sd.BufferDesc.Height = height; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.BufferDesc.RefreshRate.Numerator = 60; sd.BufferDesc.RefreshRate.Denominator = 1; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.OutputWindow = g_hWnd; sd.SampleDesc.Count = 4; sd.SampleDesc.Quality = 0; sd.Windowed = TRUE; for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ ) { g_driverType = driverTypes[driverTypeIndex]; hr = D3D10CreateDeviceAndSwapChain1( NULL, g_driverType, NULL, createDeviceFlags, D3D10_FEATURE_LEVEL_10_1, D3D10_1_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice ); if( SUCCEEDED( hr ) ) break; } if( FAILED( hr ) ) return hr; // Create a render target view ID3D10Texture2D* pBuffer; hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), ( LPVOID* )&pBuffer ); if( FAILED( hr ) ) return hr; hr = g_pd3dDevice->CreateRenderTargetView( pBuffer, NULL, &g_pRenderTargetView ); pBuffer->Release(); if( FAILED( hr ) ) return hr; g_pd3dDevice->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL ); // Setup the viewport D3D10_VIEWPORT vp; vp.Width = width; vp.Height = height; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; vp.TopLeftX = 0; vp.TopLeftY = 0; g_pd3dDevice->RSSetViewports( 1, &vp ); // Create the effect DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3D10_SHADER_DEBUG; #endif hr = D3DX10CreateEffectFromFile( "Tutorial07.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0, g_pd3dDevice, NULL, NULL, &g_pEffect, NULL, NULL ); if( FAILED( hr ) ) { MessageBox( NULL, "The FX file cannot be located. Please run this executable from the directory that contains the FX file.", "Error", MB_OK ); return hr; } // Obtain the technique g_pTechnique = g_pEffect->GetTechniqueByName( "Render" ); // Obtain the variables g_pWorldVariable = g_pEffect->GetVariableByName( "World" )->AsMatrix(); g_pViewVariable = g_pEffect->GetVariableByName( "View" )->AsMatrix(); g_pProjectionVariable = g_pEffect->GetVariableByName( "Projection" )->AsMatrix(); g_pMeshColorVariable = g_pEffect->GetVariableByName( "vMeshColor" )->AsVector(); g_pDiffuseVariable = g_pEffect->GetVariableByName( "txDiffuse" )->AsShaderResource(); // Define the input layout D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof( layout ) / sizeof( layout[0] ); // Create the input layout D3D10_PASS_DESC PassDesc; g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc ); hr = g_pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ); if( FAILED( hr ) ) return hr; // Set the input layout g_pd3dDevice->IASetInputLayout( g_pVertexLayout ); // Create vertex buffer SimpleVertex vertices[] = { // top { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, }; D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( SimpleVertex ) * 24; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; InitData.pSysMem = vertices; hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer ); if( FAILED( hr ) ) return hr; // Set vertex buffer UINT stride = sizeof( SimpleVertex ); UINT offset = 0; g_pd3dDevice->IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset ); // Create index buffer // Create vertex buffer DWORD indices[] = { 3,1,0, 2,1,3, 6,4,5, 7,4,6, 11,9,8, 10,9,11, 14,12,13, 15,12,14, 19,17,16, 18,17,19, 22,20,21, 23,20,22 }; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( DWORD ) * 36; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; InitData.pSysMem = indices; hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIndexBuffer ); if( FAILED( hr ) ) return hr; // Set index buffer g_pd3dDevice->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 ); // Set primitive topology g_pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); // Load the Texture //hr = D3DX10CreateShaderResourceViewFromFile( g_pd3dDevice, L"seafloor.dds", NULL, NULL, &g_pTextureRV, NULL ); //if( FAILED( hr ) ) // return hr; // Initialize the world matrices D3DXMatrixIdentity( &g_World ); // Initialize the view matrix D3DXVECTOR3 Eye( 0.0f, 1.5f, -2.5f ); D3DXVECTOR3 At( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 Up( 0.0f, 1.0f, 0.0f ); D3DXMatrixLookAtLH( &g_View, &Eye, &At, &Up ); // Initialize the projection matrix D3DXMatrixPerspectiveFovLH( &g_Projection, ( float )D3DX_PI * 0.25f, width / ( FLOAT )height, 0.1f, 100.0f ); // Update Variables that never change g_pViewVariable->SetMatrix( ( float* )&g_View ); g_pProjectionVariable->SetMatrix( ( float* )&g_Projection ); HANDLE texHandle = NULL; DWORD d3dFormat; ID3D10Texture2D *pTexture = NULL; HWND win = NULL; do { win = FindWindowEx(NULL, win, "Chrome_WidgetWin_0", NULL); } while (GetWindowTextLength(win) == 0); texHandle = DwmaxxGetWindowSharedHandle((HWND)win); if (texHandle == NULL) { MessageBox(NULL, "Unable to get window texture!!!!", "Error", MB_OK); return S_FALSE; } HRESULT hrs = g_pd3dDevice->OpenSharedResource(texHandle, __uuidof(ID3D10Texture2D), (void**)&pTexture); D3D10_TEXTURE2D_DESC desc; pTexture->GetDesc( &desc ); D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc; D3D10_RESOURCE_DIMENSION type; srvDesc.Format = desc.Format; srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MipLevels = desc.MipLevels; srvDesc.Texture2D.MostDetailedMip = desc.MipLevels -1; ID3D10ShaderResourceView *pSRView = NULL; g_pd3dDevice->CreateShaderResourceView( pTexture, &srvDesc, &pSRView ); g_pDiffuseVariable->SetResource( pSRView ); return S_OK; }
bool D10State::init() { static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10CreateEffectFromMemory); static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10CreateStateBlock); static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10StateBlockMaskEnableAll); if (pD3D10CreateEffectFromMemory == NULL || pD3D10CreateStateBlock == NULL || pD3D10StateBlockMaskEnableAll == NULL) { ods("D3D10: Could not get handles for all required D3D10 state initialization functions"); return false; } HRESULT hr; D3D10_STATE_BLOCK_MASK StateBlockMask; ZeroMemory(&StateBlockMask, sizeof(StateBlockMask)); hr = pD3D10StateBlockMaskEnableAll(&StateBlockMask); if (FAILED(hr)) { ods("D3D10: D3D10StateBlockMaskEnableAll failed"); return false; } hr = pD3D10CreateStateBlock(pDevice, &StateBlockMask, &pOrigStateBlock); if (FAILED(hr)) { ods("D3D10: D3D10CreateStateBlock for pOrigStateBlock failed"); return false; } hr = pD3D10CreateStateBlock(pDevice, &StateBlockMask, &pMyStateBlock); if (FAILED(hr)) { ods("D3D10: D3D10CreateStateBlock for pMyStateBlock failed"); return false; } hr = pOrigStateBlock->Capture(); if (FAILED(hr)) { ods("D3D10: Failed to store original state block during init"); return false; } ID3D10Texture2D *pBackBuffer = NULL; hr = pSwapChain->GetBuffer(0, __uuidof(*pBackBuffer), (LPVOID*)&pBackBuffer); if (FAILED(hr)) { ods("D3D10: pSwapChain->GetBuffer failure!"); return false; } pDevice->ClearState(); D3D10_TEXTURE2D_DESC backBufferSurfaceDesc; pBackBuffer->GetDesc(&backBufferSurfaceDesc); ZeroMemory(&vp, sizeof(vp)); vp.Width = backBufferSurfaceDesc.Width; vp.Height = backBufferSurfaceDesc.Height; vp.MinDepth = 0; vp.MaxDepth = 1; vp.TopLeftX = 0; vp.TopLeftY = 0; pDevice->RSSetViewports(1, &vp); hr = pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRTV); if (FAILED(hr)) { ods("D3D10: pDevice->CreateRenderTargetView failed!"); return false; } pDevice->OMSetRenderTargets(1, &pRTV, NULL); // Settings for an "over" operation. // https://en.wikipedia.org/w/index.php?title=Alpha_compositing&oldid=580659153#Description D3D10_BLEND_DESC blend; ZeroMemory(&blend, sizeof(blend)); blend.BlendEnable[0] = TRUE; blend.SrcBlend = D3D10_BLEND_ONE; blend.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; blend.BlendOp = D3D10_BLEND_OP_ADD; blend.SrcBlendAlpha = D3D10_BLEND_ONE; blend.DestBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA; blend.BlendOpAlpha = D3D10_BLEND_OP_ADD; blend.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; hr = pDevice->CreateBlendState(&blend, &pBlendState); if (FAILED(hr)) { ods("D3D10: pDevice->CreateBlendState failed!"); return false; } pDevice->OMSetBlendState(pBlendState, NULL, 0xffffffff); hr = pD3D10CreateEffectFromMemory((void *) g_main, sizeof(g_main), 0, pDevice, NULL, &pEffect); if (FAILED(hr)) { ods("D3D10: D3D10CreateEffectFromMemory failed!"); return false; } pTechnique = pEffect->GetTechniqueByName("Render"); if (pTechnique == NULL) { ods("D3D10: Could not get technique for name 'Render'"); return false; } pDiffuseTexture = pEffect->GetVariableByName("txDiffuse")->AsShaderResource(); if (pDiffuseTexture == NULL) { ods("D3D10: Could not get variable by name 'txDiffuse'"); return false; } pTexture = NULL; pSRView = NULL; // Define the input layout D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof(layout) / sizeof(layout[0]); // Create the input layout D3D10_PASS_DESC PassDesc; hr = pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc); if (FAILED(hr)) { ods("D3D10: Couldn't get pass description for technique"); return false; } hr = pDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &pVertexLayout); if (FAILED(hr)) { ods("D3D10: pDevice->CreateInputLayout failure!"); return false; } pDevice->IASetInputLayout(pVertexLayout); D3D10_BUFFER_DESC bd; ZeroMemory(&bd, sizeof(bd)); bd.Usage = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(SimpleVertex) * 4; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; hr = pDevice->CreateBuffer(&bd, NULL, &pVertexBuffer); if (FAILED(hr)) { ods("D3D10: pDevice->CreateBuffer failure!"); return false; } DWORD indices[] = { 0,1,3, 1,2,3, }; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof(DWORD) * 6; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; ZeroMemory(&InitData, sizeof(InitData)); InitData.pSysMem = indices; hr = pDevice->CreateBuffer(&bd, &InitData, &pIndexBuffer); if (FAILED(hr)) { ods("D3D10: pDevice->CreateBuffer failure!"); return false; } // Set index buffer pDevice->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0); // Set primitive topology pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); hr = pMyStateBlock->Capture(); if (FAILED(hr)) { ods("D3D10: Failed to capture newly created state block"); return false; } hr = pOrigStateBlock->Apply(); if (FAILED(hr)) { ods("D3D10: Failed to restore original state block during init"); return false; } pBackBuffer->Release(); return true; }
ID3D10ShaderResourceView * TextureHandler::getTextureArraySize(SizeTypes sizeType) const { ID3D10Texture2D *GPUTexArray = NULL; D3D10_TEXTURE2D_DESC arrayDesc; ZeroMemory(&arrayDesc, sizeof(D3D10_TEXTURE2D_DESC)); if(sizeType == SizeType_128) { if(mTextures128.empty()) { return NULL; } for(unsigned int i=0; i<mTextures128.size(); i++) { ID3D10Texture2D * texture = mTextures128[i]->getCPUTexture(); D3D10_TEXTURE2D_DESC texDesc; texture->GetDesc(&texDesc); if(i == 0) { ZeroMemory(&arrayDesc, sizeof(D3D10_TEXTURE2D_DESC)); arrayDesc.Width = 128; arrayDesc.Height = 128; arrayDesc.MipLevels = 1; arrayDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; arrayDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE; arrayDesc.Usage = D3D10_USAGE_DEFAULT; arrayDesc.ArraySize = mTextures128.size(); arrayDesc.SampleDesc = texDesc.SampleDesc; arrayDesc.CPUAccessFlags = 0; arrayDesc.MiscFlags = 0; if(FAILED(mpDevice->CreateTexture2D(&arrayDesc, NULL, &GPUTexArray))) { ERROR_MESSAGE("Failed to create GPUTexArray."); return NULL; } } D3D10_BOX box; box.left = 0; box.right = texDesc.Width; box.top = 0; box.bottom = texDesc.Height; box.front = 0; box.back = 1; D3D10_MAPPED_TEXTURE2D texMap; texture->Map(0,D3D10_MAP_READ,0,&texMap); mpDevice->UpdateSubresource(GPUTexArray, D3D10CalcSubresource(0,i,1), &box, texMap.pData, texMap.RowPitch, 1); texture->Unmap(0); } D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc; ZeroMemory(&SRVDesc, sizeof(D3D10_SHADER_RESOURCE_VIEW_DESC)); SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2DARRAY; SRVDesc.Format = arrayDesc.Format; SRVDesc.Texture2DArray.ArraySize = mTextures128.size(); SRVDesc.Texture2DArray.FirstArraySlice = 0; SRVDesc.Texture2DArray.MipLevels = 1; SRVDesc.Texture2DArray.MostDetailedMip = 0; ID3D10ShaderResourceView *texArraySRV = NULL; HRESULT error = mpDevice->CreateShaderResourceView(GPUTexArray, &SRVDesc, &texArraySRV); if(FAILED(error)) { ERROR_MESSAGE("Failed to create Tex2DArray ShaderResourceView."); ERROR_MESSAGE(DXGetErrorDescriptionA(error)); return NULL; } return texArraySRV; } else if(sizeType == SizeType_256) { if(mTextures256.empty()) { return NULL; } for(unsigned int i=0; i<mTextures256.size(); i++) { ID3D10Texture2D * texture = mTextures256[i]->getCPUTexture(); D3D10_TEXTURE2D_DESC texDesc; texture->GetDesc(&texDesc); if(i == 0) { ZeroMemory(&arrayDesc, sizeof(D3D10_TEXTURE2D_DESC)); arrayDesc.Width = 256; arrayDesc.Height = 256; arrayDesc.MipLevels = 1; arrayDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; arrayDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE; arrayDesc.Usage = D3D10_USAGE_DEFAULT; arrayDesc.ArraySize = mTextures256.size(); arrayDesc.SampleDesc = texDesc.SampleDesc; arrayDesc.CPUAccessFlags = 0; arrayDesc.MiscFlags = 0; if(FAILED(mpDevice->CreateTexture2D(&arrayDesc, NULL, &GPUTexArray))) { ERROR_MESSAGE("Failed to create GPUTexArray."); return NULL; } } D3D10_BOX box; box.left = 0; box.right = texDesc.Width; box.top = 0; box.bottom = texDesc.Height; box.front = 0; box.back = 1; D3D10_MAPPED_TEXTURE2D texMap; texture->Map(0,D3D10_MAP_READ,0,&texMap); mpDevice->UpdateSubresource(GPUTexArray, D3D10CalcSubresource(0,i,1), &box, texMap.pData, texMap.RowPitch, 1); texture->Unmap(0); } D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc; ZeroMemory(&SRVDesc, sizeof(D3D10_SHADER_RESOURCE_VIEW_DESC)); SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2DARRAY; SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; SRVDesc.Texture2DArray.ArraySize = mTextures256.size(); SRVDesc.Texture2DArray.FirstArraySlice = 0; SRVDesc.Texture2DArray.MipLevels = 1; SRVDesc.Texture2DArray.MostDetailedMip = 0; ID3D10ShaderResourceView *texArraySRV = NULL; HRESULT error = mpDevice->CreateShaderResourceView(GPUTexArray, &SRVDesc, &texArraySRV); if(FAILED(error)) { ERROR_MESSAGE("Failed to create Tex2DArray ShaderResourceView."); ERROR_MESSAGE(DXGetErrorDescriptionA(error)); return NULL; } return texArraySRV; }else if(sizeType == SizeType_512) { if(mTextures512.empty()) { return NULL; } for(unsigned int i=0; i<mTextures512.size(); i++) { ID3D10Texture2D * texture = mTextures512[i]->getCPUTexture(); D3D10_TEXTURE2D_DESC texDesc; texture->GetDesc(&texDesc); if(i == 0) { ZeroMemory(&arrayDesc, sizeof(D3D10_TEXTURE2D_DESC)); arrayDesc.Width = 512; arrayDesc.Height = 512; arrayDesc.MipLevels = 1; arrayDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; arrayDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE; arrayDesc.Usage = D3D10_USAGE_DEFAULT; arrayDesc.ArraySize = mTextures512.size(); arrayDesc.SampleDesc = texDesc.SampleDesc; arrayDesc.CPUAccessFlags = 0; arrayDesc.MiscFlags = 0; if(FAILED(mpDevice->CreateTexture2D(&arrayDesc, NULL, &GPUTexArray))) { ERROR_MESSAGE("Failed to create GPUTexArray."); return NULL; } } D3D10_BOX box; box.left = 0; box.right = texDesc.Width; box.top = 0; box.bottom = texDesc.Height; box.front = 0; box.back = 1; D3D10_MAPPED_TEXTURE2D texMap; texture->Map(0,D3D10_MAP_READ,0,&texMap); mpDevice->UpdateSubresource(GPUTexArray, D3D10CalcSubresource(0,i,1), &box, texMap.pData, texMap.RowPitch, 1); texture->Unmap(0); } D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc; ZeroMemory(&SRVDesc, sizeof(D3D10_SHADER_RESOURCE_VIEW_DESC)); SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2DARRAY; SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; SRVDesc.Texture2DArray.ArraySize = mTextures512.size(); SRVDesc.Texture2DArray.FirstArraySlice = 0; SRVDesc.Texture2DArray.MipLevels = 1; SRVDesc.Texture2DArray.MostDetailedMip = 0; ID3D10ShaderResourceView *texArraySRV = NULL; HRESULT error = mpDevice->CreateShaderResourceView(GPUTexArray, &SRVDesc, &texArraySRV); if(FAILED(error)) { ERROR_MESSAGE("Failed to create Tex2DArray ShaderResourceView."); ERROR_MESSAGE(DXGetErrorDescriptionA(error)); return NULL; } return texArraySRV; } else if(sizeType == SizeType_1024) { if(mTextures1024.empty()) { return NULL; } for(unsigned int i=0; i<mTextures1024.size(); i++) { ID3D10Texture2D * texture = mTextures1024[i]->getCPUTexture(); D3D10_TEXTURE2D_DESC texDesc; texture->GetDesc(&texDesc); if(i == 0) { ZeroMemory(&arrayDesc, sizeof(D3D10_TEXTURE2D_DESC)); arrayDesc.Width = 1024; arrayDesc.Height = 1024; arrayDesc.MipLevels = 1; arrayDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; arrayDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE; arrayDesc.Usage = D3D10_USAGE_DEFAULT; arrayDesc.ArraySize = mTextures1024.size(); arrayDesc.SampleDesc = texDesc.SampleDesc; arrayDesc.CPUAccessFlags = 0; arrayDesc.MiscFlags = 0; if(FAILED(mpDevice->CreateTexture2D(&arrayDesc, NULL, &GPUTexArray))) { ERROR_MESSAGE("Failed to create GPUTexArray."); return NULL; } } D3D10_BOX box; box.left = 0; box.right = texDesc.Width; box.top = 0; box.bottom = texDesc.Height; box.front = 0; box.back = 1; D3D10_MAPPED_TEXTURE2D texMap; texture->Map(0,D3D10_MAP_READ,0,&texMap); mpDevice->UpdateSubresource(GPUTexArray, D3D10CalcSubresource(0,i,1), &box, texMap.pData, texMap.RowPitch, 1); texture->Unmap(0); } D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc; ZeroMemory(&SRVDesc, sizeof(D3D10_SHADER_RESOURCE_VIEW_DESC)); SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2DARRAY; SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; SRVDesc.Texture2DArray.ArraySize = mTextures1024.size(); SRVDesc.Texture2DArray.FirstArraySlice = 0; SRVDesc.Texture2DArray.MipLevels = 1; SRVDesc.Texture2DArray.MostDetailedMip = 0; ID3D10ShaderResourceView *texArraySRV = NULL; HRESULT error = mpDevice->CreateShaderResourceView(GPUTexArray, &SRVDesc, &texArraySRV); if(FAILED(error)) { ERROR_MESSAGE("Failed to create Tex2DArray ShaderResourceView."); ERROR_MESSAGE(DXGetErrorDescriptionA(error)); return NULL; } return texArraySRV; } return NULL; }
void TextureHandler::addTexture(std::string fileName, std::string textureName) { ID3D10ShaderResourceView * rv; ID3D10Texture2D * GPUTexture; ID3D10Texture2D * CPUTexture; D3DX10_IMAGE_LOAD_INFO loadInfo; ZeroMemory(&loadInfo,sizeof(D3DX10_IMAGE_LOAD_INFO)); loadInfo.Width = D3DX10_DEFAULT; loadInfo.Height = D3DX10_DEFAULT; loadInfo.BindFlags = 0; loadInfo.Depth = D3DX10_DEFAULT; loadInfo.Filter = D3DX10_DEFAULT; loadInfo.FirstMipLevel = D3DX10_DEFAULT; loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM; loadInfo.MipFilter = D3DX10_DEFAULT; loadInfo.MiscFlags = D3DX10_DEFAULT; loadInfo.pSrcInfo = 0; loadInfo.Usage = D3D10_USAGE_STAGING; loadInfo.CpuAccessFlags = D3D10_CPU_ACCESS_READ | D3D10_CPU_ACCESS_WRITE; HRESULT hr = D3DX10CreateTextureFromFile(mpDevice, fileName.c_str(), &loadInfo, NULL, (ID3D10Resource**)&CPUTexture, NULL ); if(FAILED(hr)) { ERROR_MESSAGE("Failed to create CPU texture from file: "<<fileName); return; } D3D10_TEXTURE2D_DESC texDesc; CPUTexture->GetDesc(&texDesc); if(texDesc.Width > 1024) { ERROR_MESSAGE("Can't load textures wider than 1024 pixels. filename: "<<fileName); return; } else if(texDesc.Height > 1024) { ERROR_MESSAGE("Can't load textures higher than 1024 pixels. filename: "<<fileName); return; } SizeTypes type; if(texDesc.Width > 512 || texDesc.Height > 512) { type = TextureHandler::SizeType_1024; } else if(texDesc.Width > 256 || texDesc.Height > 256) { type = TextureHandler::SizeType_512; } else if(texDesc.Width > 128 || texDesc.Height > 128) { type = TextureHandler::SizeType_256; } else { type = TextureHandler::SizeType_128; } D3D10_MAPPED_TEXTURE2D texMap; CPUTexture->Map(0, D3D10_MAP_WRITE, 0, &texMap); ((byte*)texMap.pData)[0] = percent(texDesc.Width, type); ((byte*)texMap.pData)[1] = percent(texDesc.Height, type); ((byte*)texMap.pData)[texDesc.Width] = 255; ZeroMemory(&loadInfo,sizeof(D3DX10_IMAGE_LOAD_INFO)); loadInfo.Width = D3DX10_DEFAULT; loadInfo.Height = D3DX10_DEFAULT; loadInfo.BindFlags = D3D10_BIND_SHADER_RESOURCE; loadInfo.Depth = D3DX10_DEFAULT; loadInfo.Filter = D3DX10_DEFAULT; loadInfo.FirstMipLevel = D3DX10_DEFAULT; loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM; loadInfo.MipFilter = D3DX10_DEFAULT; loadInfo.MiscFlags = D3DX10_DEFAULT; loadInfo.pSrcInfo = 0; loadInfo.Usage = D3D10_USAGE_DEFAULT; loadInfo.CpuAccessFlags = 0; hr = D3DX10CreateTextureFromFile(mpDevice, fileName.c_str(), &loadInfo, NULL, (ID3D10Resource**)&GPUTexture, NULL ); if(FAILED(hr)) { ERROR_MESSAGE("Failed to create GPU texture from file: "<<fileName); return; } byte * n00b = myNew byte[1024 * 1024 * 4]; for(unsigned int y=0; y<texDesc.Height; y++) { for(unsigned int x=0; x<texDesc.Width; x++) { n00b[y * texDesc.Width * 4 + x * 4 + 0] = ((byte*)texMap.pData)[y * texDesc.Width * 4 + x * 4 + 0]; n00b[y * texDesc.Width * 4 + x * 4 + 1] = ((byte*)texMap.pData)[y * texDesc.Width * 4 + x * 4 + 1]; n00b[y * texDesc.Width * 4 + x * 4 + 2] = ((byte*)texMap.pData)[y * texDesc.Width * 4 + x * 4 + 2]; n00b[y * texDesc.Width * 4 + x * 4 + 3] = 255; } } CPUTexture->Unmap(0); //n00b[0] = 255;//percent(texDesc.Width); //n00b[1] = 255;//100;//percent(texDesc.Height); mpDevice->UpdateSubresource(GPUTexture, D3D10CalcSubresource(0, 0, 1), NULL, n00b, texDesc.Width * 4, texDesc.Width * texDesc.Height * 4); SAFE_DELETE(n00b); D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; viewDesc.Format = texDesc.Format; viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; viewDesc.Texture2D.MipLevels = 1; viewDesc.Texture2D.MostDetailedMip = 0; hr = mpDevice->CreateShaderResourceView(GPUTexture,&viewDesc,&rv); GPUTexture->Release(); if(FAILED(hr)) { ERROR_MESSAGE("Failed to create GPU resource View. Filename: "<<fileName); ERROR_MESSAGE(DXGetErrorDescriptionA(hr)); return; } Texture * texture = myNew Texture(rv, CPUTexture, textureName); if(texDesc.Width > 512 || texDesc.Height > 512) { mTextures1024.push_back(texture); } else if(texDesc.Width > 256 || texDesc.Height > 256) { mTextures512.push_back(texture); } else if(texDesc.Width > 128 || texDesc.Height > 128) { mTextures256.push_back(texture); } else { mTextures128.push_back(texture); } mTextures.push_back(texture); }
Texture* D3D10Texture::CreateFromSharedHandle(unsigned int width, unsigned int height, HANDLE handle) { HRESULT err; if(!handle) { AppWarning(TEXT("D3D10Texture::CreateFromSharedHandle: NULL handle value.")); return NULL; } ID3D10Resource *tempResource; if(FAILED(err = GetD3D()->OpenSharedResource(handle, __uuidof(ID3D10Resource), (void**)&tempResource))) { AppWarning(TEXT("D3D10Texture::CreateFromSharedHandle: Failed to open shared handle, result = 0x%08lX"), err); return NULL; } ID3D10Texture2D *texVal; if(FAILED(err = tempResource->QueryInterface(__uuidof(ID3D10Texture2D), (void**)&texVal))) { SafeRelease(tempResource); AppWarning(TEXT("D3D10Texture::CreateFromSharedHandle: could not query interface, result = 0x%08lX"), err); return NULL; } tempResource->Release(); //------------------------------------------ D3D10_TEXTURE2D_DESC td; texVal->GetDesc(&td); //------------------------------------------ D3D10_SHADER_RESOURCE_VIEW_DESC resourceDesc; zero(&resourceDesc, sizeof(resourceDesc)); resourceDesc.Format = td.Format; //resourceDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; resourceDesc.Texture2D.MipLevels = 1; //resourceDesc.ViewDimension = D3D10_1_SRV_DIMENSION_TEXTURE2D; resourceDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D; ID3D10ShaderResourceView *resource = NULL; if(FAILED(err = GetD3D()->CreateShaderResourceView(texVal, &resourceDesc, &resource))) { SafeRelease(texVal); AppWarning(TEXT("D3D10Texture::CreateFromSharedHandle: CreateShaderResourceView failed, result = 0x%08lX"), err); return NULL; } //------------------------------------------ D3D10Texture *newTex = new D3D10Texture; newTex->format = GetGSFormatFromDXGIFormat(td.Format);; newTex->resource = resource; newTex->texture = texVal; newTex->bDynamic = false; newTex->width = width; newTex->height = height; return newTex; }
void D10State::init() { static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10CreateEffectFromMemory); static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10CreateStateBlock); static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10StateBlockMaskEnableAll); HRESULT hr; dwMyThread = GetCurrentThreadId(); D3D10_STATE_BLOCK_MASK StateBlockMask; ZeroMemory(&StateBlockMask, sizeof(StateBlockMask)); pD3D10StateBlockMaskEnableAll(&StateBlockMask); pD3D10CreateStateBlock(pDevice, &StateBlockMask, &pOrigStateBlock); pD3D10CreateStateBlock(pDevice, &StateBlockMask, &pMyStateBlock); pOrigStateBlock->Capture(); ID3D10Texture2D* pBackBuffer = NULL; hr = pSwapChain->GetBuffer(0, __uuidof(*pBackBuffer), (LPVOID*)&pBackBuffer); pDevice->ClearState(); D3D10_TEXTURE2D_DESC backBufferSurfaceDesc; pBackBuffer->GetDesc(&backBufferSurfaceDesc); ZeroMemory(&vp, sizeof(vp)); vp.Width = backBufferSurfaceDesc.Width; vp.Height = backBufferSurfaceDesc.Height; vp.MinDepth = 0; vp.MaxDepth = 1; vp.TopLeftX = 0; vp.TopLeftY = 0; pDevice->RSSetViewports(1, &vp); hr = pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRTV); pDevice->OMSetRenderTargets(1, &pRTV, NULL); D3D10_BLEND_DESC blend; ZeroMemory(&blend, sizeof(blend)); blend.BlendEnable[0] = TRUE; blend.SrcBlend = D3D10_BLEND_ONE; blend.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; blend.BlendOp = D3D10_BLEND_OP_ADD; blend.SrcBlendAlpha = D3D10_BLEND_ONE; blend.DestBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA; blend.BlendOpAlpha = D3D10_BLEND_OP_ADD; blend.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; pDevice->CreateBlendState(&blend, &pBlendState); float bf[4]; pDevice->OMSetBlendState(pBlendState, bf, 0xffffffff); pD3D10CreateEffectFromMemory((void *) g_main, sizeof(g_main), 0, pDevice, NULL, &pEffect); pTechnique = pEffect->GetTechniqueByName("Render"); pDiffuseTexture = pEffect->GetVariableByName("txDiffuse")->AsShaderResource(); pTexture = NULL; pSRView = NULL; // Define the input layout D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof(layout) / sizeof(layout[0]); // Create the input layout D3D10_PASS_DESC PassDesc; pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc); hr = pDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &pVertexLayout); pDevice->IASetInputLayout(pVertexLayout); D3D10_BUFFER_DESC bd; ZeroMemory(&bd, sizeof(bd)); bd.Usage = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(SimpleVertex) * 4; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; hr = pDevice->CreateBuffer(&bd, NULL, &pVertexBuffer); DWORD indices[] = { 0,1,3, 1,2,3, }; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof(DWORD) * 6; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; ZeroMemory(&InitData, sizeof(InitData)); InitData.pSysMem = indices; hr = pDevice->CreateBuffer(&bd, &InitData, &pIndexBuffer); // Set index buffer pDevice->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0); // Set primitive topology pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); pMyStateBlock->Capture(); pOrigStateBlock->Apply(); pBackBuffer->Release(); dwMyThread = 0; }