Author Topic: ShaderResourceView cube map from six images  (Read 333 times)

Caché GB

  • Member
  • **
  • Posts: 70
  • MASM IS HOT
ShaderResourceView cube map from six images
« on: July 04, 2018, 05:48:14 AM »
This is how to load a dxll ShaderResourceView as a cube map (Box) in MASM32
from six individual images.

All images are of the same pixel size and are usaly 512 by 512 or for better
quality use 1024 by 1024. The technique shown here does not make use of image
staging (.MipLevels = 1) as it is not necessary when used as a sky cube/sphere
since the image's Z will always be 1 (furthest from camera). However, some games
that allow for different playable screen resolutions may make use of image staging
to adjust for quality.

The 6 images (2D textures) have one MipLevel only and are assembled as a cube map
array represented as:-

                  (+Y)
            (-X)  (+Z)  (+X) (-Z)
                  (-Y)


Look ma, no D3DX 11 utility library.

Code: [Select]
mAm MACRO m1:REQ, m2:REQ     ; memory to memory using 'A'ccumulator i.e. EAX register
     mov  eax, m2
     mov  m1, eax
ENDM

.DATA

    ;;  The 6 individual images where:
    ;;  (+X)_right, (-X)_left, (+Y)_up, (-Y)_down, (+Z)_front, (-Z)_back

    swPX   dw  L(Assets\\posx_rt.jpg\0)     
    swNX   dw  L(Assets\\negx_lf.jpg\0)
    swPY   dw  L(Assets\\posy_up.jpg\0)
    swNY   dw  L(Assets\\negy_dn.jpg\0)
    swPZ   dw  L(Assets\\posz_ft.jpg\0)
    swNZ   dw  L(Assets\\negz_bk.jpg\0)      ;; Useing the L macro by Ernest Murphy

   g_pCubeShaderResourceView  LPID3D11ShaderResourceView  null

.CODE

;; Note: (1) - GdiplusStartup already done.
;;       (2) - Error checking removed.
;;       (3) - g_pd3dDevice is a valed public pointer to a ID3D11Device Interface.
;;       (4) - The callee saves registers esi and ebx in the 2 .while loops using Gdi Plus.

CreateCubeShaderResourceView PROC

    local  ShaderResDesc:D3D11_SHADER_RESOURCE_VIEW_DESC
    local  Texture2dDesc:D3D11_TEXTURE2D_DESC
    local  pTextureArray:LPID3D11Texture2D
    local  InitData[6]:D3D11_SUBRESOURCE_DATA
    local  GdipBitmapData[6]:BitmapData
    local  pImage[6]:ptr
    local  pFile[6]:ptr
    local  i:dword

          mov  pFile[0*4], offset swPX
          mov  pFile[1*4], offset swNX
          mov  pFile[2*4], offset swPY
          mov  pFile[3*4], offset swNY
          mov  pFile[4*4], offset swPZ
          mov  pFile[5*4], offset swNZ

          lea  ebx, GdipBitmapData
          mov  i, 0
       .while (i < 6)
             mov  esi, i
          invoke  GdipCreateBitmapFromFile, pFile[esi*4], addr pImage[esi*4]
          invoke  GdipBitmapLockBits, pImage[esi*4], null, ImageLockModeRead, PixelFormat32bppARGB, ebx
             add  ebx,  sizeof(BitmapData)
             inc  i
       .endw

          mAm  Texture2dDesc.dWidth, GdipBitmapData[0].dWidth    ;; All images are of the same size.
          mAm  Texture2dDesc.dHeight, GdipBitmapData[0].dHeight
          mov  Texture2dDesc.MipLevels, 1
          mov  Texture2dDesc.ArraySize, 6                        ;; <-- Note: for 6 images
          mov  Texture2dDesc.Format, DXGI_FORMAT_B8G8R8A8_UNORM
          mov  Texture2dDesc.SampleDesc.Count, 1
          mov  Texture2dDesc.SampleDesc.Quality, 0
          mov  Texture2dDesc.CPUAccessFlags, 0
          mov  Texture2dDesc.Usage, D3D11_USAGE_DEFAULT
          mov  Texture2dDesc.BindFlags, D3D11_BIND_SHADER_RESOURCE
          mov  Texture2dDesc.MiscFlags, D3D11_RESOURCE_MISC_TEXTURECUBE

          mAm  ShaderResDesc.Format, Texture2dDesc.Format
          mov  ShaderResDesc.ViewDimension, D3D11_SRV_DIMENSION_TEXTURECUBE
          mov  ShaderResDesc.Texture2D.MostDetailedMip, 0
          mAm  ShaderResDesc.Texture2D.MipLevels, Texture2dDesc.MipLevels
          mov  ShaderResDesc.Texture2DArray.FirstArraySlice, 0               ;; "Zero out rest
          mov  ShaderResDesc.Texture2DArray.ArraySize, 0                     ;;  of the union."

          mov  i, 0
       .while (i < 6)
          mov  esi, sizeof(D3D11_SUBRESOURCE_DATA)
         imul  esi, i
          mov  ebx, sizeof(BitmapData)
         imul  ebx, i
          mAm  InitData[esi].pSysMem, GdipBitmapData[ebx].Scan0
          mAm  InitData[esi].SysMemPitch, GdipBitmapData[ebx].Stride
          mov  InitData[esi].SysMemSlicePitch, 0                             ;; Use only for 3d textures else set to 0.
          inc  i
       .endw       

      coinvoke  g_pd3dDevice, ID3D11Device, CreateTexture2D, addr Texture2dDesc, addr InitData[0], addr pTextureArray

      coinvoke  g_pd3dDevice, ID3D11Device, CreateShaderResourceView, pTextureArray, addr ShaderResDesc, addr g_pCubeShaderResourceView
     
      coinvoke  pTextureArray, ID3D11Texture2D, Release

          lea  ebx, GdipBitmapData
          mov  i, 0
       .while (i < 6)
             mov  esi, i
          invoke  GdipBitmapUnlockBits, pImage[esi*4], ebx
          invoke  GdipDisposeImage, pImage[esi*4]
             add  ebx,  sizeof(BitmapData)
             inc  i
       .endw

       return  true

CreateCubeShaderResourceView ENDP

;#############################################################################################################

Caché GB's 1 and 0-nly language:MASM

Siekmanski

  • Member
  • *****
  • Posts: 1684
Re: ShaderResourceView cube map from six images
« Reply #1 on: July 04, 2018, 02:22:24 PM »
Quote
Look ma, no D3DX 11 utility library.

 :bgrin:
Creative coders use backward thinking techniques as a strategy.