void Texture::CreateTextureFromFile(ID3D12GraphicsCommandList* gfx_command_list, const char* file) { std::streampos size; uint8* tex_data; shared_context.log->LogText(LogLevel::DEBUG_PRINT, "Loading texture: %s", file); std::ifstream file_stream(file, std::ios::in | std::ios::binary | std::ios::ate); if (file_stream.is_open()) { size = file_stream.tellg(); tex_data = new uint8[size]; file_stream.seekg(0, std::ios::beg); file_stream.read((char*)tex_data, size); file_stream.close(); CreateDDSTextureFromMemory(gfx_command_list, tex_data, (uint32)size, &m_texture, &m_uploadTexture); if(m_texture != nullptr && m_uploadTexture != nullptr) { m_GPUhandle = shared_context.gfx_device->GetDescHeapCBV_SRV()->GetGPUHandleAtHead(); shared_context.log->LogText(LogLevel::SUCCESS, "Texture loaded: %s", file); } delete[] tex_data; } else { shared_context.log->LogText(LogLevel::FATAL_ERROR, "Texture load failed: %s", file); } }
texture::texture(const std::string& filename,ID3D11Device* device) { unsigned int file_length = 0; unsigned char* file_ba = texhelper::loadfile(filename,file_length); textype filetextype = texhelper::gettextype(file_ba); switch(filetextype) { case textype::DDS : CreateDDSTextureFromMemory( device, file_ba, file_length, nullptr, &m_textureview, MAXSIZE_T ); break; case textype::PNG : { unsigned int width,height; unsigned char* ba_rgba8888 = texhelper::pngtorgba8888( file_ba, width, height ); CreateRawTextureFromMemory( device, &m_textureview , (void*) ba_rgba8888 , jff::RAW_8888 , width , height ); } break; case textype::JPG : //CreateRawTextureFromMemory(); break; }; delete[] file_ba; }
void TextureLib::InitInstance(ID3D11Device* device) { s_Inst = new TextureLib(); Imple* pImple = s_Inst->m_pImple; // color format ABGR pImple->m_defaultTextures[TextureType::DIFFUSE] = CreateCheckerboardTexture2D(device, 128, 128, 0xFF404040, 0xFF808080); pImple->m_defaultTextures[TextureType::Cubemap] = CreateCheckerboardTexture2D(device, 128, 128, 0xff000040, 0xff000080, true); pImple->m_defaultTextures[TextureType::NORMAL] = CreateSolidTexture2D(device, 8, 8, 0xFFFF8080); pImple->m_defaultTextures[TextureType::LIGHT] = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF); pImple->m_defaultTextures[TextureType::SPEC] = CreateSolidTexture2D(device, 8, 8, 0xFF000000); pImple->m_defaultTextures[TextureType::BlankMask] = CreateSolidTexture2D(device, 4, 4, 0x00); pImple->m_defaultTextures[TextureType::FullMask] = CreateSolidTexture2D(device, 4, 4, 0xFFFFFFFF); pImple->m_whiteTexture = CreateSolidTexture2D(device, 8, 8, 0xFFFFFFFF); typedef std::pair<std::wstring, Texture*> NameTexPair; uint32_t resCount = ResUtil::ResourceCount(); for(uint32_t i = 0; i < resCount; i++) { const wchar_t* resName = ResUtil::GetResourceName(i); const std::wstring ext = FileUtils::GetExtensionLower(resName); HRESULT hr = E_FAIL; Texture* tex = NULL; ID3D11Resource* dxresource = NULL; ID3D11ShaderResourceView* dxTexView = NULL; uint32_t resSize = 0; if(ext == L".dds") { uint8_t* data = ResUtil::LoadResource(resName,&resSize); if(resSize == 0) continue; hr = CreateDDSTextureFromMemory( device, data, resSize, &dxresource, &dxTexView); free(data); } else if(ext == L".png" || ext == L".bmp" || ext == L".jpeg") { uint8_t* data = ResUtil::LoadResource(resName,&resSize); if(resSize == 0) continue; hr = CreateWICTextureFromMemory( device, NULL, data, resSize, &dxresource, &dxTexView); free(data); } if (Logger::IsFailureLog(hr, L"Error loading %s\n", resName)) { continue; } D3D11_RESOURCE_DIMENSION resType = D3D11_RESOURCE_DIMENSION_UNKNOWN; dxresource->GetType( &resType ); assert( resType == D3D11_RESOURCE_DIMENSION_TEXTURE2D); ID3D11Texture2D* dxTex = NULL; hr = dxresource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &dxTex ); dxresource->Release(); assert(dxTex); tex = new Texture(dxTex,dxTexView); auto insertResult = pImple->m_textures.insert(NameTexPair(resName,tex)); assert(insertResult.second); } }
void TextureStore::loadTexture(wstring filename, string id) { TextureLoadResult result; TextureInfo initialTexture; // only use to initialize struct in texture store - do not access this after assignment to store vector<byte> file_buffer; initialTexture.id = id; textures[id] = initialTexture; TextureInfo *texture = &textures[id]; // find texture file, look in pak file first: PakEntry *pakFileEntry = nullptr; pakFileEntry = xapp().findFileInPak(filename.c_str()); // try file system if not found in pak: initialTexture.filename = filename; // TODO check: field not needed? only in this method? --> remove if (pakFileEntry == nullptr) { wstring binFile = xapp().findFile(filename.c_str(), XApp::TEXTURE); texture->filename = binFile; //initialTexture.filename = binFile; xapp().readFile(texture->filename.c_str(), file_buffer, XApp::FileCategory::TEXTURE); } else { xapp().readFile(pakFileEntry, file_buffer, XApp::FileCategory::TEXTURE); } ID3D12GraphicsCommandList *commandList = this->commandList.Get(); //D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV // create heap - TODO adjust for one heap for multiple textures // Describe and create a shader resource view (SRV) heap for the texture. D3D12_DESCRIPTOR_HEAP_DESC srvHeapDesc = {}; srvHeapDesc.NumDescriptors = 1; srvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; srvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; ThrowIfFailed(xapp().device->CreateDescriptorHeap(&srvHeapDesc, IID_PPV_ARGS(&texture->m_srvHeap))); CD3DX12_CPU_DESCRIPTOR_HANDLE srvHandle(texture->m_srvHeap->GetCPUDescriptorHandleForHeapStart()); //CD3DX12_CPU_DESCRIPTOR_HANDLE(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); CreateDDSTextureFromMemory( xapp().device.Get(), &file_buffer[0], file_buffer.size(), 0, true, &texture->texSRV, srvHandle, result ); //CreateDDSTextureFromFile( // xapp().device.Get(), // texture->filename.c_str(), // 0, // true, // &texture->texSRV, // srvHandle, // result // ); // upload texture to GPU: //ID3D12Resource* UploadBuffer; UINT64 uploadBufferSize = GetRequiredIntermediateSize(texture->texSRV.Get(), 0, result.NumSubresources); //CommandContext& InitContext = CommandContext::Begin(); D3D12_HEAP_PROPERTIES HeapProps; HeapProps.Type = D3D12_HEAP_TYPE_UPLOAD; HeapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; HeapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; HeapProps.CreationNodeMask = 1; HeapProps.VisibleNodeMask = 1; D3D12_RESOURCE_DESC BufferDesc; BufferDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; BufferDesc.Alignment = 0; BufferDesc.Width = uploadBufferSize; BufferDesc.Height = 1; BufferDesc.DepthOrArraySize = 1; BufferDesc.MipLevels = 1; BufferDesc.Format = DXGI_FORMAT_UNKNOWN; BufferDesc.SampleDesc.Count = 1; BufferDesc.SampleDesc.Quality = 0; BufferDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; BufferDesc.Flags = D3D12_RESOURCE_FLAG_NONE; ThrowIfFailed(xapp().device->CreateCommittedResource(&HeapProps, D3D12_HEAP_FLAG_NONE, &BufferDesc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&result.UploadBuffer))); // copy data to the intermediate upload heap and then schedule a copy from the upload heap to the default texture //InitContext.TransitionResource(Dest, D3D12_RESOURCE_STATE_COPY_DEST, true); commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(texture->texSRV.Get(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST)); UpdateSubresources(commandList, texture->texSRV.Get(), result.UploadBuffer, 0, 0, result.NumSubresources, result.initData.get()); commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(texture->texSRV.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE)); //InitContext.TransitionResource(Dest, D3D12_RESOURCE_STATE_GENERIC_READ, true); // Execute the command list and wait for it to finish so we can release the upload buffer //InitContext.CloseAndExecute(true); //UploadBuffer->Release(); ThrowIfFailed(commandList->Close()); ID3D12CommandList* ppCommandLists[] = { this->commandList.Get() }; xapp().commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists); // Create synchronization objects and wait until assets have been uploaded to the GPU. //Sleep(300); EffectBase::createSyncPoint(updateFrameData, xapp().commandQueue); EffectBase::waitForSyncPoint(updateFrameData); //auto &f = frameData[frameIndex]; //createSyncPoint(f, xapp().commandQueue); //waitForSyncPoint(f); result.UploadBuffer->Release(); ThrowIfFailed(commandList->Reset(commandAllocator.Get(), pipelineState.Get())); }