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; }
void VertexShaderCache::Init() { const D3D11_INPUT_ELEMENT_DESC simpleelems[2] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; const D3D11_INPUT_ELEMENT_DESC clearelems[2] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; unsigned int cbsize = ((sizeof(vsconstants))&(~0xf))+0x10; // must be a multiple of 16 D3D11_BUFFER_DESC cbdesc = CD3D11_BUFFER_DESC(cbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); HRESULT hr = D3D::device->CreateBuffer(&cbdesc, NULL, &vscbuf); CHECK(hr==S_OK, "Create vertex shader constant buffer (size=%u)", cbsize); D3D::SetDebugObjectName((ID3D11DeviceChild*)vscbuf, "vertex shader constant buffer used to emulate the GX pipeline"); D3DBlob* blob; D3D::CompileVertexShader(simple_shader_code, sizeof(simple_shader_code), &blob); D3D::device->CreateInputLayout(simpleelems, 2, blob->Data(), blob->Size(), &SimpleLayout); SimpleVertexShader = D3D::CreateVertexShaderFromByteCode(blob); if (SimpleLayout == NULL || SimpleVertexShader == NULL) PanicAlert("Failed to create simple vertex shader or input layout at %s %d\n", __FILE__, __LINE__); blob->Release(); D3D::SetDebugObjectName((ID3D11DeviceChild*)SimpleVertexShader, "simple vertex shader"); D3D::SetDebugObjectName((ID3D11DeviceChild*)SimpleLayout, "simple input layout"); D3D::CompileVertexShader(clear_shader_code, sizeof(clear_shader_code), &blob); D3D::device->CreateInputLayout(clearelems, 2, blob->Data(), blob->Size(), &ClearLayout); ClearVertexShader = D3D::CreateVertexShaderFromByteCode(blob); if (ClearLayout == NULL || ClearVertexShader == NULL) PanicAlert("Failed to create clear vertex shader or input layout at %s %d\n", __FILE__, __LINE__); blob->Release(); D3D::SetDebugObjectName((ID3D11DeviceChild*)ClearVertexShader, "clear vertex shader"); D3D::SetDebugObjectName((ID3D11DeviceChild*)ClearLayout, "clear input layout"); Clear(); // these values are hardcoded, they depend on internal D3DCompile behavior // TODO: Do this with D3DReflect or something instead unsigned int k; for (k = 0;k < 6;k++) vs_constant_offset_table[C_POSNORMALMATRIX+k] = 0+4*k; for (k = 0;k < 4;k++) vs_constant_offset_table[C_PROJECTION+k] = 24+4*k; for (k = 0;k < 4;k++) vs_constant_offset_table[C_MATERIALS+k] = 40+4*k; for (k = 0;k < 40;k++) vs_constant_offset_table[C_LIGHTS+k] = 56+4*k; for (k = 0;k < 24;k++) vs_constant_offset_table[C_TEXMATRICES+k] = 216+4*k; for (k = 0;k < 64;k++) vs_constant_offset_table[C_TRANSFORMMATRICES+k] = 312+4*k; for (k = 0;k < 32;k++) vs_constant_offset_table[C_NORMALMATRICES+k] = 568+4*k; for (k = 0;k < 64;k++) vs_constant_offset_table[C_POSTTRANSFORMMATRICES+k] = 696+4*k; vs_constant_offset_table[C_DEPTHPARAMS] = 952; if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX))) File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX).c_str()); SETSTAT(stats.numVertexShadersCreated, 0); SETSTAT(stats.numVertexShadersAlive, 0); char cache_filename[MAX_PATH]; sprintf(cache_filename, "%sdx11-%s-vs.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(), SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str()); VertexShaderCacheInserter inserter; g_vs_disk_cache.OpenAndRead(cache_filename, inserter); if (g_Config.bEnableShaderDebugging) Clear(); last_entry = NULL; }