void InitUtils() { util_vbuf = new UtilVertexBuffer(65535); float border[4] = { 0.f, 0.f, 0.f, 0.f }; D3D11_SAMPLER_DESC samDesc = CD3D11_SAMPLER_DESC(D3D11_FILTER_MIN_MAG_MIP_POINT, D3D11_TEXTURE_ADDRESS_BORDER, D3D11_TEXTURE_ADDRESS_BORDER, D3D11_TEXTURE_ADDRESS_BORDER, 0.f, 1, D3D11_COMPARISON_ALWAYS, border, 0.f, 0.f); HRESULT hr = D3D::device->CreateSamplerState(&samDesc, ToAddr(point_copy_sampler)); if (FAILED(hr)) PanicAlert("Failed to create sampler state at %s %d\n", __FILE__, __LINE__); else SetDebugObjectName(point_copy_sampler.get(), "point copy sampler state"); samDesc = CD3D11_SAMPLER_DESC(D3D11_FILTER_MIN_MAG_MIP_LINEAR, D3D11_TEXTURE_ADDRESS_BORDER, D3D11_TEXTURE_ADDRESS_BORDER, D3D11_TEXTURE_ADDRESS_BORDER, 0.f, 1, D3D11_COMPARISON_ALWAYS, border, 0.f, 0.f); hr = D3D::device->CreateSamplerState(&samDesc, ToAddr(linear_copy_sampler)); if (FAILED(hr)) PanicAlert("Failed to create sampler state at %s %d\n", __FILE__, __LINE__); else SetDebugObjectName(linear_copy_sampler.get(), "linear copy sampler state"); // cached data used to avoid unnecessarily reloading the vertex buffers memset(&tex_quad_data, 0, sizeof(tex_quad_data)); memset(&draw_quad_data, 0, sizeof(draw_quad_data)); memset(&clear_quad_data, 0, sizeof(clear_quad_data)); // make sure to properly load the vertex data whenever the corresponding functions get called the first time stq_observer = stsq_observer = cq_observer = clearq_observer = true; util_vbuf->AddWrapObserver(&stq_observer); util_vbuf->AddWrapObserver(&stsq_observer); util_vbuf->AddWrapObserver(&cq_observer); util_vbuf->AddWrapObserver(&clearq_observer); font.Init(); }
ComputeShaderPtr CreateComputeShaderFromByteCode(const void* bytecode, size_t len) { ComputeShaderPtr c_shader; HRESULT hr = D3D::device->CreateComputeShader(bytecode, len, nullptr, ToAddr(c_shader)); if (FAILED(hr)) { PanicAlert("CreateComputeShaderFromByteCode failed at %s %d\n", __FILE__, __LINE__); } return c_shader; }
void BBox::Init() { if (g_ActiveConfig.backend_info.bSupportsBBox) { // Create 2 buffers here. // First on Default pool. auto desc = CD3D11_BUFFER_DESC(4 * sizeof(s32), D3D11_BIND_UNORDERED_ACCESS, D3D11_USAGE_DEFAULT, 0, 0, 4); s_values[0] = s_values[1] = s_values[2] = s_values[3] = 0; D3D11_SUBRESOURCE_DATA data; data.pSysMem = s_values; data.SysMemPitch = 4 * sizeof(s32); data.SysMemSlicePitch = 0; HRESULT hr; hr = D3D::device->CreateBuffer(&desc, &data, ToAddr(s_bbox_buffer)); CHECK(SUCCEEDED(hr), "Create BoundingBox buffer."); D3D::SetDebugObjectName(s_bbox_buffer.get(), "BoundingBox buffer"); // Second to use as a staging buffer. desc.Usage = D3D11_USAGE_STAGING; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.BindFlags = 0; hr = D3D::device->CreateBuffer(&desc, nullptr, ToAddr(s_bbox_staging_buffer)); CHECK(SUCCEEDED(hr), "Create BoundingBox staging buffer."); D3D::SetDebugObjectName(s_bbox_staging_buffer.get(), "BoundingBox staging buffer"); // UAV is required to allow concurrent access. D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc = {}; UAVdesc.Format = DXGI_FORMAT_R32_SINT; UAVdesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; UAVdesc.Buffer.FirstElement = 0; UAVdesc.Buffer.Flags = 0; UAVdesc.Buffer.NumElements = 4; hr = D3D::device->CreateUnorderedAccessView(s_bbox_buffer.get(), &UAVdesc, ToAddr(s_bbox_uav)); CHECK(SUCCEEDED(hr), "Create BoundingBox UAV."); s_cpu_dirty = true; s_gpu_dirty = true; } }
CTestMessageHandler::CTestMessageHandler() { CMockAuthShort::CreateInstanceNoInit(_spAuthShort.GetPointerPointer()); CMockAuthLong::CreateInstanceNoInit(_spAuthLong.GetPointerPointer()); ToAddr(c_szIPLocal, c_portLocal, &_addrLocal); ToAddr(c_szIPMapped, c_portMapped, &_addrMapped); ToAddr(c_szIPServerPrimary, c_portServerPrimary, &_addrServerPP); ToAddr(c_szIPServerPrimary, c_portServerAlternate, &_addrServerPA); ToAddr(c_szIPServerAlternate, c_portServerPrimary, &_addrServerAP); ToAddr(c_szIPServerAlternate, c_portServerAlternate, &_addrServerAA); }
// code->bytecode bool CompileShader( ShaderType type, const std::string& code, D3DBlob& blob, const D3D_SHADER_MACRO* pDefines, const char* pEntry, bool throwerror) { #if defined(_DEBUG) || defined(DEBUGFAST) UINT flags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION; #else UINT flags = D3DCOMPILE_SKIP_VALIDATION; if (type != DX11::D3D::ShaderType::Hull) { flags |= D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3DCOMPILE_OPTIMIZATION_LEVEL3; } else { flags |= D3DCOMPILE_SKIP_OPTIMIZATION; } #endif char const *profile = nullptr; char const *sufix = nullptr; switch (type) { case DX11::D3D::ShaderType::Vertex: profile = D3D::VertexShaderVersionString(); sufix = "vs"; break; case DX11::D3D::ShaderType::Pixel: profile = D3D::PixelShaderVersionString(); sufix = "ps"; break; case DX11::D3D::ShaderType::Geometry: profile = D3D::GeometryShaderVersionString(); sufix = "gs"; break; case DX11::D3D::ShaderType::Hull: profile = D3D::HullShaderVersionString(); sufix = "hs"; break; case DX11::D3D::ShaderType::Domain: profile = D3D::DomainShaderVersionString(); sufix = "ds"; break; case DX11::D3D::ShaderType::Compute: profile = D3D::ComputeShaderVersionString(); sufix = "cs"; break; default: return false; break; } ID3DBlobPtr shaderBuffer; ID3DBlobPtr errorBuffer; HRESULT hr = HLSLCompiler::getInstance().CompileShader(code.c_str(), code.length(), nullptr, pDefines, nullptr, pEntry != nullptr ? pEntry : "main", profile, flags, 0, ToAddr(shaderBuffer), ToAddr(errorBuffer)); if (errorBuffer) { INFO_LOG(VIDEO, "Shader compiler messages:\n%s", (const char*)errorBuffer->GetBufferPointer()); } if (FAILED(hr)) { static int num_failures = 0; std::string filename = StringFromFormat("%sbad_%s_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), sufix, num_failures++); std::ofstream file; OpenFStream(file, filename, std::ios_base::out); file << code; file << "\n"; file << (const char*)errorBuffer->GetBufferPointer(); file.close(); if (throwerror) { PanicAlert("Failed to compile shader: %s\nDebug info (%s):\n%s", filename.c_str(), profile, (char*)errorBuffer->GetBufferPointer()); } blob = nullptr; } else { blob = std::move(shaderBuffer); } return SUCCEEDED(hr); }
int CD3DFont::Init() { // Create vertex buffer for the letters HRESULT hr; // Prepare to create a bitmap unsigned int* pBitmapBits; BITMAPINFO bmi; ZeroMemory(&bmi.bmiHeader, sizeof(BITMAPINFOHEADER)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = (int)m_dwTexWidth; bmi.bmiHeader.biHeight = -(int)m_dwTexHeight; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biBitCount = 32; // Create a DC and a bitmap for the font HDC hDC = CreateCompatibleDC(nullptr); HBITMAP hbmBitmap = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS, (void**)&pBitmapBits, nullptr, 0); SetMapMode(hDC, MM_TEXT); // create a GDI font HFONT hFont = CreateFont(24, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH, _T("Tahoma")); if (nullptr == hFont) return E_FAIL; HGDIOBJ hOldbmBitmap = SelectObject(hDC, hbmBitmap); HGDIOBJ hOldFont = SelectObject(hDC, hFont); // Set text properties SetTextColor(hDC, 0xFFFFFF); SetBkColor(hDC, 0); SetTextAlign(hDC, TA_TOP); TEXTMETRICW tm; GetTextMetricsW(hDC, &tm); m_LineHeight = tm.tmHeight; // Loop through all printable characters and output them to the bitmap // Meanwhile, keep track of the corresponding tex coords for each character. int x = 0, y = 0; char str[2] = "\0"; for (int c = 0; c < 127 - 32; c++) { str[0] = c + 32; SIZE size; GetTextExtentPoint32A(hDC, str, 1, &size); if ((int)(x + size.cx + 1) > m_dwTexWidth) { x = 0; y += m_LineHeight; } ExtTextOutA(hDC, x + 1, y + 0, ETO_OPAQUE | ETO_CLIPPED, nullptr, str, 1, nullptr); m_fTexCoords[c][0] = ((float)(x + 0)) / m_dwTexWidth; m_fTexCoords[c][1] = ((float)(y + 0)) / m_dwTexHeight; m_fTexCoords[c][2] = ((float)(x + 0 + size.cx)) / m_dwTexWidth; m_fTexCoords[c][3] = ((float)(y + 0 + size.cy)) / m_dwTexHeight; x += size.cx + 3; // 3 to work around annoying ij conflict (part of the j ends up with the i) } // Create a new texture for the font // possible optimization: store the converted data in a buffer and fill the texture on creation. // That way, we can use a static texture ID3D11Texture2D* buftex; D3D11_TEXTURE2D_DESC texdesc = CD3D11_TEXTURE2D_DESC(D3D::GetBaseBufferFormat(), m_dwTexWidth, m_dwTexHeight, 1, 1, D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); hr = device->CreateTexture2D(&texdesc, nullptr, &buftex); if (FAILED(hr)) { PanicAlert("Failed to create font texture"); return hr; } D3D::SetDebugObjectName(buftex, "texture of a CD3DFont object"); // Lock the surface and write the alpha values for the set pixels D3D11_MAPPED_SUBRESOURCE texmap; hr = context->Map(buftex, 0, D3D11_MAP_WRITE_DISCARD, 0, &texmap); if (FAILED(hr)) PanicAlert("Failed to map a texture at %s %d\n", __FILE__, __LINE__); for (y = 0; y < m_dwTexHeight; y++) { u32* pDst32 = (u32*)((u8*)texmap.pData + y * texmap.RowPitch); for (x = 0; x < m_dwTexWidth; x++) { const u8 bAlpha = (pBitmapBits[m_dwTexWidth * y + x] & 0xff); *pDst32++ = (((bAlpha << 4) | bAlpha) << 24) | 0xFFFFFF; } } // Done updating texture, so clean up used objects context->Unmap(buftex, 0); hr = D3D::device->CreateShaderResourceView(buftex, nullptr, ToAddr(m_pTexture)); if (FAILED(hr)) PanicAlert("Failed to create shader resource view at %s %d\n", __FILE__, __LINE__); SAFE_RELEASE(buftex); SelectObject(hDC, hOldbmBitmap); DeleteObject(hbmBitmap); SelectObject(hDC, hOldFont); DeleteObject(hFont); // setup device objects for drawing m_pshader = D3D::CompileAndCreatePixelShader(fontpixshader); if (m_pshader == nullptr) PanicAlert("Failed to create pixel shader, %s %d\n", __FILE__, __LINE__); D3D::SetDebugObjectName(m_pshader.get(), "pixel shader of a CD3DFont object"); D3DBlob vsbytecode; D3D::CompileShader(DX11::D3D::ShaderType::Vertex, fontvertshader, vsbytecode); if (vsbytecode.Data() == nullptr) PanicAlert("Failed to compile vertex shader, %s %d\n", __FILE__, __LINE__); m_vshader = D3D::CreateVertexShaderFromByteCode(vsbytecode); if (m_vshader.get() == nullptr) PanicAlert("Failed to create vertex shader, %s %d\n", __FILE__, __LINE__); D3D::SetDebugObjectName(m_vshader.get(), "vertex shader of a CD3DFont object"); const D3D11_INPUT_ELEMENT_DESC desc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; hr = D3D::device->CreateInputLayout(desc, 3, vsbytecode.Data(), vsbytecode.Size(), ToAddr(m_InputLayout)); if (FAILED(hr)) PanicAlert("Failed to create input layout, %s %d\n", __FILE__, __LINE__); D3D11_BLEND_DESC blenddesc; blenddesc.AlphaToCoverageEnable = FALSE; blenddesc.IndependentBlendEnable = FALSE; blenddesc.RenderTarget[0].BlendEnable = TRUE; blenddesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; blenddesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; blenddesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; blenddesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; blenddesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; hr = D3D::device->CreateBlendState(&blenddesc, ToAddr(m_blendstate)); CHECK(hr == S_OK, "Create font blend state"); D3D::SetDebugObjectName(m_blendstate.get(), "blend state of a CD3DFont object"); D3D11_RASTERIZER_DESC rastdesc = CD3D11_RASTERIZER_DESC(D3D11_FILL_SOLID, D3D11_CULL_NONE, false, 0, 0.f, 0.f, false, false, false, false); hr = D3D::device->CreateRasterizerState(&rastdesc, ToAddr(m_raststate)); CHECK(hr == S_OK, "Create font rasterizer state"); D3D::SetDebugObjectName(m_raststate.get(), "rasterizer state of a CD3DFont object"); D3D11_BUFFER_DESC vbdesc = CD3D11_BUFFER_DESC(MAX_NUM_VERTICES*sizeof(FONT2DVERTEX), D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); if (FAILED(hr = device->CreateBuffer(&vbdesc, nullptr, ToAddr(m_pVB)))) { PanicAlert("Failed to create font vertex buffer at %s, line %d\n", __FILE__, __LINE__); return hr; } D3D::SetDebugObjectName(m_pVB.get(), "vertex buffer of a CD3DFont object"); return S_OK; }