view RenderD3D11.cpp @ 2489:ceda74e07201

.
author Ritor1
date Thu, 11 Sep 2014 17:30:59 +0600
parents 0683c0a38282
children
line wrap: on
line source

#include <d3dcompiler.h>
#include <comdef.h> // _com_error

#include "ErrorHandling.h"
#include "RenderD3D11.h"
#include "mm7_data.h"

#define ErrorD3D(x)\
{\
  HRESULT hr = x;\
  if (FAILED(hr))\
  {\
    _com_error com_error(hr);\
    Error("HRESULT = %08X\n%S", hr, com_error.ErrorMessage());\
  }\
}


RenderD3D11::RenderD3D11() {}
RenderD3D11::~RenderD3D11() {}


void RenderD3D11::ClearBlack() {__debugbreak();}
void RenderD3D11::SaveWinnersCertificate(const char *a1) {__debugbreak();}
void RenderD3D11::Present() {__debugbreak();}
void RenderD3D11::_49FD3A_fullscreen() {__debugbreak();}
bool RenderD3D11::InitializeFullscreen() {__debugbreak(); return 0;}
void RenderD3D11::CreateZBuffer() {__debugbreak();}
void RenderD3D11::Release() {__debugbreak();}
void RenderD3D11::RasterLine2D(signed int uX, signed int uY, signed int uZ, signed int uW, unsigned __int16 uColor) {__debugbreak();}
void RenderD3D11::ClearZBuffer(int a2, int a3) {__debugbreak();}
void RenderD3D11::SetRasterClipRect(unsigned int uX, unsigned int uY, unsigned int uZ, unsigned int uW) {__debugbreak();}
bool RenderD3D11::LockSurface_DDraw4(IDirectDrawSurface4 *pSurface, DDSURFACEDESC2 *pDesc, unsigned int uLockFlags) {__debugbreak(); return 0;}
void RenderD3D11::GetTargetPixelFormat(DDPIXELFORMAT *pOut) {__debugbreak();}
void RenderD3D11::LockRenderSurface(void **pOutSurfacePtr, unsigned int *pOutPixelsPerRow) {__debugbreak();}
void RenderD3D11::UnlockBackBuffer() {__debugbreak();}
void RenderD3D11::LockFrontBuffer(void **pOutSurface, unsigned int *pOutPixelsPerRow) {__debugbreak();}
void RenderD3D11::UnlockFrontBuffer() {__debugbreak();}
void RenderD3D11::RestoreFrontBuffer() {__debugbreak();}
void RenderD3D11::RestoreBackBuffer() {__debugbreak();}
void RenderD3D11::BltToFront(RECT *pDstRect, IDirectDrawSurface *pSrcSurface, RECT *pSrcRect, unsigned int uBltFlags) {__debugbreak();}
void RenderD3D11::BltBackToFontFast(int a2, int a3, RECT *a4) {__debugbreak();}
void RenderD3D11::BeginSceneD3D() {__debugbreak();}
unsigned int RenderD3D11::GetActorTintColor(float a2, int tint, int a4, int a5, RenderBillboard *a6) {__debugbreak(); return 0;}
void RenderD3D11::DrawPolygon(unsigned int uNumVertices, struct Polygon *a3, ODMFace *a4, IDirect3DTexture2 *pTexture) {__debugbreak();}
void RenderD3D11::DrawTerrainPolygon(unsigned int uNumVertices, struct Polygon *a4, IDirect3DTexture2 *a5, bool transparent, bool clampAtTextureBorders) {__debugbreak();}
void RenderD3D11::DrawIndoorPolygon(unsigned int uNumVertices, struct BLVFace *a3, IDirect3DTexture2 *pHwTex, struct Texture *pTex, int uPackedID, unsigned int uColor, int a8) {__debugbreak();}
void RenderD3D11::MakeParticleBillboardAndPush_BLV(RenderBillboardTransform_local0 *a2, IDirect3DTexture2 *a3, unsigned int uDiffuse, int angle) {__debugbreak();}
void RenderD3D11::MakeParticleBillboardAndPush_ODM(RenderBillboardTransform_local0 *a2, IDirect3DTexture2 *a3, unsigned int uDiffuse, int angle) {__debugbreak();}
void RenderD3D11::DrawBillboards_And_MaybeRenderSpecialEffects_And_EndScene() {__debugbreak();}
void RenderD3D11::DrawBillboard_Indoor(RenderBillboardTransform_local0 *pSoftBillboard, Sprite *pSprite, int dimming_level) {__debugbreak();}
void RenderD3D11::_4A4CC9_AddSomeBillboard(struct stru6_stru1_indoor_sw_billboard *a1, int diffuse) {__debugbreak();}
void RenderD3D11::TransformBillboardsAndSetPalettesODM() {__debugbreak();}
void RenderD3D11::DrawBillboardList_BLV() {__debugbreak();}
void RenderD3D11::DrawProjectile(float srcX, float srcY, float a3, float a4, float dstX, float dstY, float a7, float a8, IDirect3DTexture2 *a9) {__debugbreak();}
bool RenderD3D11::LoadTexture(const char *pName, unsigned int bMipMaps, IDirectDrawSurface4 **pOutSurface, IDirect3DTexture2 **pOutTexture) {__debugbreak(); return 0;}
bool RenderD3D11::MoveSpriteToDevice(Sprite *pSprite) {__debugbreak(); return 0;}
void RenderD3D11::ScreenFade(unsigned int color, float t) {__debugbreak();}
void RenderD3D11::SetTextureClipRect(unsigned int uX, unsigned int uY, unsigned int uZ, unsigned int uW) {__debugbreak();}
void RenderD3D11::ResetTextureClipRect() {__debugbreak();}
void RenderD3D11::CreditsTextureScroll(unsigned int pX, unsigned int pY, int move_X, int move_Y, RGBTexture *pTexture) {__debugbreak();}
void RenderD3D11::DrawTextureIndexed(unsigned int uX, unsigned int uY, struct Texture *a4) {__debugbreak();}
void RenderD3D11::ZBuffer_Fill_2(signed int a2, signed int a3, struct Texture *pTexture, int a5) {__debugbreak();}
void RenderD3D11::DrawMaskToZBuffer(signed int uOutX, unsigned int uOutY, struct Texture *pTexture, int zVal) {__debugbreak();}
void RenderD3D11::DrawTextureTransparent(unsigned int uX, unsigned int uY, struct Texture *pTexture) {__debugbreak();}
void RenderD3D11::DrawAura(unsigned int a2, unsigned int a3, struct Texture *a4, struct Texture *a5, int a6, int a7, int a8) {__debugbreak();}
void RenderD3D11::_4A65CC(unsigned int x, unsigned int y, struct Texture *a4, struct Texture *a5, int a6, int a7, int a8) {__debugbreak();}
void RenderD3D11::DrawTransparentRedShade(unsigned int a2, unsigned int a3, struct Texture *a4) {__debugbreak();}
void RenderD3D11::DrawTransparentGreenShade(signed int a2, signed int a3, struct Texture *pTexture) {__debugbreak();}
void RenderD3D11::DrawFansTransparent(const RenderVertexD3D3 *vertices, unsigned int num_vertices) {__debugbreak();}
void RenderD3D11::DrawMasked(signed int a2, signed int a3, struct Texture *pTexture, unsigned __int16 mask) {__debugbreak();}
void RenderD3D11::GetLeather(unsigned int a2, unsigned int a3, struct Texture *a4, __int16 height) {__debugbreak();}
void RenderD3D11::DrawTextPalette(int x, int y, unsigned char* font_pixels, int a5, unsigned int uFontHeight, unsigned __int16 *pPalette, int a8) {__debugbreak();}
void RenderD3D11::DrawText(signed int uOutX, signed int uOutY, unsigned __int8 *pFontPixels, unsigned int uCharWidth, unsigned int uCharHeight, unsigned __int16 *pFontPalette, unsigned __int16 uFaceColor, unsigned __int16 uShadowColor) {__debugbreak();}
void RenderD3D11::FillRectFast(unsigned int uX, unsigned int uY, unsigned int uWidth, unsigned int uHeight, unsigned int uColor16) {__debugbreak();}
void RenderD3D11::_4A6DF5(unsigned __int16 *pBitmap, unsigned int uBitmapPitch, struct Vec2_int_ *pBitmapXY, void *pTarget, unsigned int uTargetPitch, Vec4_int_ *a7) {__debugbreak();}
void RenderD3D11::DrawTranslucent(unsigned int a2, unsigned int a3, struct Texture *a4) {__debugbreak();}
void RenderD3D11::DrawBuildingsD3D() {__debugbreak();}
void RenderD3D11::DrawIndoorSky(unsigned int uNumVertices, unsigned int uFaceID) {__debugbreak();}
void RenderD3D11::DrawOutdoorSkyD3D() {__debugbreak();}
void RenderD3D11::DrawOutdoorSkyPolygon(unsigned int uNumVertices, struct Polygon *pSkyPolygon, IDirect3DTexture2 *pTexture) {__debugbreak();}
void RenderD3D11::DrawIndoorSkyPolygon(signed int uNumVertices, struct Polygon *pSkyPolygon, IDirect3DTexture2 *pTexture) {__debugbreak();}
void RenderD3D11::PrepareDecorationsRenderList_ODM() {__debugbreak();}
void RenderD3D11::DrawSpriteObjects_ODM() {__debugbreak();}
void RenderD3D11::RenderTerrainD3D() {__debugbreak();}
void RenderD3D11::ChangeBetweenWinFullscreenModes() {__debugbreak();}
bool RenderD3D11::AreRenderSurfacesOk() {__debugbreak(); return 0;}
void RenderD3D11::SaveScreenshot(const char *pFilename, unsigned int width, unsigned int height) {__debugbreak();}
void RenderD3D11::PackScreenshot(unsigned int width, unsigned int height, void *out_data, unsigned int data_size, unsigned int *screenshot_size) {__debugbreak();}
void RenderD3D11::SavePCXScreenshot() {__debugbreak();}
int RenderD3D11::_46À6ÀÑ_GetActorsInViewport(int pDepth) {__debugbreak(); return 0;}
void RenderD3D11::BeginLightmaps() {__debugbreak();}
void RenderD3D11::EndLightmaps() {__debugbreak();}
void RenderD3D11::BeginLightmaps2() {__debugbreak();}
void RenderD3D11::EndLightmaps2() {__debugbreak();}
bool RenderD3D11::DrawLightmap(struct Lightmap *pLightmap, struct Vec3_float_ *pColorMult, float z_bias) {__debugbreak(); return 0;}
void RenderD3D11::BeginDecals() {__debugbreak();}
void RenderD3D11::EndDecals() {__debugbreak();}
void RenderD3D11::DrawDecal(struct Decal *pDecal, float z_bias) {__debugbreak();}
void RenderD3D11::do_draw_debug_line_d3d(const RenderVertexD3D3 *pLineBegin, signed int sDiffuseBegin, const RenderVertexD3D3 *pLineEnd, signed int sDiffuseEnd, float z_stuff) {__debugbreak();}
void RenderD3D11::DrawLines(const RenderVertexD3D3 *vertices, unsigned int num_vertices) {__debugbreak();}
void RenderD3D11::DrawSpecialEffectsQuad(const RenderVertexD3D3 *vertices, IDirect3DTexture2 *texture) {__debugbreak();}
void RenderD3D11::am_Blt_Copy(RECT *pSrcRect, POINT *pTargetXY, int a3) {__debugbreak();}
void RenderD3D11::am_Blt_Chroma(RECT *pSrcRect, POINT *pTargetPoint, int a3, int blend_mode) {__debugbreak();}

void RenderD3D11::Sub01() {__debugbreak();}





void RenderD3D11::PresentBlackScreen()
{
  ClearTarget(0xFF000000);
  ErrorD3D(pSwapChain->Present(0, 0));
}

void RenderD3D11::BeginScene() {}
void RenderD3D11::EndScene() {}

void RenderD3D11::ClearTarget(unsigned int uColor)
{
  float clear_color[] =
  {
    ((uColor & 0x00FF0000) >> 16) / 255.0f,
    ((uColor & 0x0000FF00) >> 8) / 255.0f,
    ((uColor & 0x000000FF) >> 0) / 255.0f,
    ((uColor & 0xFF000000) >> 24) / 255.0f
  };
  d3dc->ClearRenderTargetView(primary_srv, clear_color);
}


void RenderD3D11::DrawTextureRGB(unsigned int uOutX, unsigned int uOutY, RGBTexture *a4)
{
  __debugbreak();
}

bool RenderD3D11::IsGammaSupported()
{
  return false;
}

struct
{
  unsigned char restore_resolution;
  unsigned char  _saved_screen_bpp;
  unsigned short _saved_screen_width;
  unsigned short _saved_screen_height;
} on_exit;

void ChangeResolution(int new_width, int new_height, int new_bpp)
{
  if (!on_exit.restore_resolution)
  {
    on_exit.restore_resolution = true;

    auto hdc = GetDC(nullptr);
    {
      on_exit._saved_screen_width = GetDeviceCaps(hdc, HORZRES);
      on_exit._saved_screen_height = GetDeviceCaps(hdc, VERTRES);
      on_exit._saved_screen_bpp = GetDeviceCaps(hdc, BITSPIXEL);
    }
    ReleaseDC(nullptr, hdc);
  }

  DEVMODEA dm;
  dm.dmSize = sizeof(dm);
  dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  dm.dmBitsPerPel = new_bpp;
  dm.dmPelsWidth = new_width;
  dm.dmPelsHeight = new_height;
      
  if (ChangeDisplaySettingsA(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
    Error("ChangeDisplaySettingsA");
}

__declspec(noreturn) void ExitApp()
{
  if (on_exit.restore_resolution)
    ChangeResolution(on_exit._saved_screen_width, on_exit._saved_screen_height, on_exit._saved_screen_bpp);

  ExitProcess(0);
}



bool RenderD3D11::SwitchToWindow()
{
  if (on_exit.restore_resolution)
  {
    on_exit.restore_resolution = false;
    ChangeResolution(on_exit._saved_screen_width, on_exit._saved_screen_height, on_exit._saved_screen_bpp);
  }
  return true;
}




bool RenderD3D11::Initialize(OSWindow *window)
{
  this->window = window;

  auto d3d_lib = LoadLibraryW(L"d3d11.dll");
  if (!d3d_lib)
    return Error("d3d11.dll is missing"), false;


  DXGI_SWAP_CHAIN_DESC swapChainDesc;
  memset(&swapChainDesc, 0, sizeof(swapChainDesc));
  swapChainDesc.BufferDesc.Width = window->GetWidth();
  swapChainDesc.BufferDesc.Height = window->GetHeight();
  //swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  //swapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
  swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  //swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  //swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  swapChainDesc.SampleDesc.Count = 1;
  //swapChainDesc.SampleDesc.Quality = 0;
  swapChainDesc.BufferUsage =  DXGI_USAGE_RENDER_TARGET_OUTPUT;
  swapChainDesc.BufferCount = 2;
  swapChainDesc.OutputWindow = window->GetApiHandle();
  swapChainDesc.Windowed = true;
  //swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  //swapChainDesc.Flags = 0;


  D3D_FEATURE_LEVEL requested_feature_level =  D3D_FEATURE_LEVEL_11_0,
                    received_feature_level;

  unsigned int device_flags = 0;//D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT;
  #ifndef NODEBUG
    device_flags |= 0/*D3D11_CREATE_DEVICE_DEBUG | D3D11_CREATE_DEVICE_DEBUGGABLE*/;
   #endif

  HRESULT (__stdcall *dll_D3D11CreateDeviceAndSwapChain)(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, CONST D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext);
  dll_D3D11CreateDeviceAndSwapChain = (decltype(dll_D3D11CreateDeviceAndSwapChain))GetProcAddress(d3d_lib, "D3D11CreateDeviceAndSwapChain");
  ErrorD3D(dll_D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, device_flags, nullptr, 0, D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &d3dd, &received_feature_level, &d3dc));

  if (received_feature_level < D3D_FEATURE_LEVEL_10_0)
  {
    MessageBoxA(nullptr, "Received Direct3D 9 or lower", "", 0);
    __debugbreak();
  }
      /*if (fullscreen)
      {
        extern void ChangeResolution(int new_dith, int new_height, int new_bpp);
        ChangeResolution(target_window->GetWidth(), target_window->GetHeight(), 32);

        target_window->SetPosition(0, 0);
        target_window->SetTopmost(true);
      }
      ErrorD3D(pSwapChain->SetFullscreenState(fullscreen, nullptr));*/


  ID3D11Texture2D *pSwapChainSurface;
  {
    ErrorD3D(pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)&pSwapChainSurface));
    ErrorD3D(d3dd->CreateRenderTargetView(pSwapChainSurface, nullptr, &primary_srv));
  }
  pSwapChainSurface->Release();


  D3D11_TEXTURE2D_DESC z_desc;
  memset(&z_desc, 0, sizeof(z_desc));
  z_desc.Width = window->GetWidth();
  z_desc.Height = window->GetHeight();
  z_desc.MipLevels = 1;
  z_desc.ArraySize = 1;
  z_desc.Format = DXGI_FORMAT_D32_FLOAT;
  z_desc.SampleDesc.Count = 1;
  //z_desc.SampleDesc.Quality = 0;
  //z_desc.Usage = D3D11_USAGE_DEFAULT;
  z_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  //z_desc.CPUAccessFlags = 0;
  //z_desc.MiscFlags = 0;

  ID3D11Texture2D *depth_surface;
  ErrorD3D(d3dd->CreateTexture2D(&z_desc, nullptr, &depth_surface));

  D3D11_DEPTH_STENCIL_VIEW_DESC depth_srv_desc;
  memset(&depth_srv_desc, 0, sizeof(depth_srv_desc));
  depth_srv_desc.Format = DXGI_FORMAT_D32_FLOAT;
  depth_srv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  //depth_srv_desc.Texture2D.MipSlice = 0;
  ErrorD3D(d3dd->CreateDepthStencilView(depth_surface, &depth_srv_desc, &depth_srv));

  d3dc->OMSetRenderTargets(1, &primary_srv, depth_srv);
  d3dc->ClearDepthStencilView(depth_srv, D3D11_CLEAR_DEPTH, 1.0f, 0);



  D3D11_RASTERIZER_DESC d3drs_desc;
  memset(&d3drs_desc, 0, sizeof(d3drs_desc));
  d3drs_desc.FillMode = D3D11_FILL_SOLID;
  d3drs_desc.CullMode = D3D11_CULL_NONE;
  //d3drs_desc.FrontCounterClockwise = false;
  //d3drs_desc.DepthBias = 0;
  //d3drs_desc.DepthBiasClamp = 0.0f;
  //d3drs_desc.SlopeScaledDepthBias = 0.0f;
  //d3drs_desc.DepthClipEnable = true;
  //d3drs_desc.ScissorEnable = false;
  //d3drs_desc.MultisampleEnable = false;
  //d3drs_desc.AntialiasedLineEnable = false;

  ID3D11RasterizerState *d3drs;
  ErrorD3D(d3dd->CreateRasterizerState(&d3drs_desc, &d3drs));
  d3dc->RSSetState(d3drs);
  d3drs->Release();



  D3D11_VIEWPORT viewport;
  memset(&viewport, 0, sizeof(viewport));
  viewport.TopLeftX = game_viewport_x;
  viewport.TopLeftY = game_viewport_y;
  viewport.Width    = game_viewport_width;
  viewport.Height   = game_viewport_height;
  //viewport.MinDepth = 0;
  viewport.MaxDepth = 1;
  d3dc->RSSetViewports(1, &viewport);

  return true;
}