News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Lurking as a guest long enough

Started by Caché GB, June 11, 2018, 06:58:50 AM

Previous topic - Next topic

Caché GB

szStr   db   "Hello MASM32 world", 0

I would like to offer you all a drink but this is impossible
through email, until your 3D printer can make a whisky.

The Game Development sub forum is my favored corner.

As you all know the D3DX 11 utility library is deprecated thus NO

     D3DX11CreateShaderResourceViewFromFile

This is how to do it with pure dx11 and gdiplus in MASM.
Gdiplus because it can handel .jpg, .png and .bmp

        ;;  Note swSomeTexture is a UNICODE string i.e. LPCWSTR
        ;;  Set third parm TRUE to let dx11 Auto Generate (dx11 decides how many) MipLevels
    invoke  CreateShaderResourceViewFromFile, g_pd3dDevice, addr swSomeTexture, true, addr g_pSomeShaderResourceView

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

.code   ;; error checking removed
CreateShaderResourceViewFromFile PROC pd3d11Device:PTR, pImageFile:PTR WORD, AutoGen:DWORD, pShaderResourceView:PTR

    local  pd3d11Context:LPID3D11DeviceContext
    local  Texture2dDesc:D3D11_TEXTURE2D_DESC
    local  InitData:D3D11_SUBRESOURCE_DATA
    local  ShaderResDesc:D3D11_SHADER_RESOURCE_VIEW_DESC
    local  pd3d11Texture2D:LPID3D11Texture2D ; :LPID3D11Resource
    local  AutoGenState:ptr
    local  GdipBitmapData:BitmapData
    local  pImage:ptr

           ;;  GdiplusStartup already done
           ;;  pImageFile points to a UNICODE string

       invoke  GdipCreateBitmapFromFile, pImageFile, addr pImage

       invoke  GdipBitmapLockBits, pImage, null, ImageLockModeRead, PixelFormat32bppARGB, addr GdipBitmapData

          m2m  Texture2dDesc._Width, GdipBitmapData._Width
          m2m  Texture2dDesc.Height, GdipBitmapData.Height
          mov  Texture2dDesc.ArraySize, 1
          mov  Texture2dDesc.Format, DXGI_FORMAT_B8G8R8A8_UNORM ; DXGI_FORMAT_R8G8B8A8_UNORM 
          mov  Texture2dDesc.SampleDesc.Count, 1
          mov  Texture2dDesc.SampleDesc.Quality, 0
          mov  Texture2dDesc.CPUAccessFlags, 0

          m2m  InitData.pSysMem, GdipBitmapData.Scan0
          m2m  InitData.SysMemPitch, GdipBitmapData.Stride
          mov  eax, GdipBitmapData.Stride
         imul  eax, GdipBitmapData.Height
          mov  InitData.SysMemSlicePitch, eax

          m2m  ShaderResDesc.Format, Texture2dDesc.Format
          mov  ShaderResDesc.ViewDimension, D3D11_SRV_DIMENSION_TEXTURE2D
          mov  ShaderResDesc.Texture2D.MostDetailedMip, 0
          mov  ShaderResDesc.Texture2DArray.FirstArraySlice, 0
          mov  ShaderResDesc.Texture2DArray.ArraySize, 0

       .if (AutoGen == true)
          mov  Texture2dDesc.Usage, D3D11_USAGE_DEFAULT
          mov  Texture2dDesc.BindFlags, D3D11_BIND_SHADER_RESOURCE or D3D11_BIND_RENDER_TARGET
          mov  Texture2dDesc.MiscFlags, D3D11_RESOURCE_MISC_GENERATE_MIPS
          mov  Texture2dDesc.MipLevels, 0
          mov  ShaderResDesc.Texture2D.MipLevels, -1
          mov  AutoGenState, 0
       .else
          mov  Texture2dDesc.Usage, D3D11_USAGE_IMMUTABLE ; D3D11_USAGE_DEFAULT
          mov  Texture2dDesc.BindFlags, D3D11_BIND_SHADER_RESOURCE
          mov  Texture2dDesc.MiscFlags, 0
          mov  Texture2dDesc.MipLevels, 1
          mov  ShaderResDesc.Texture2D.MipLevels, 1
          lea  eax, InitData
          mov  AutoGenState, eax
       .endif

     coinvoke  pd3d11Device, ID3D11Device, CreateTexture2D, addr Texture2dDesc, AutoGenState, addr pd3d11Texture2D

     coinvoke  pd3d11Device, ID3D11Device, CreateShaderResourceView, pd3d11Texture2D, addr ShaderResDesc, pShaderResourceView

       invoke  GdipBitmapUnlockBits, pImage, addr GdipBitmapData

       .if (AutoGen == true)
          coinvoke  pd3d11Device, ID3D11Device, GetImmediateContext, addr pd3d11Context

          coinvoke  pd3d11Context, ID3D11DeviceContext, UpdateSubresource, pd3d11Texture2D, 0, null, \
                                 InitData.pSysMem, InitData.SysMemPitch, InitData.SysMemSlicePitch

               mov  eax, pShaderResourceView    ;; load ShaderResource pointer
               mov  eax, [eax]                               ;; and dereference it once only
          coinvoke  pd3d11Context, ID3D11DeviceContext, GenerateMips, eax
       .endif

     coinvoke  pd3d11Texture2D, ID3D11Texture2D, Release
       
       invoke  GdipDisposeImage, pImage

          ret

CreateShaderResourceViewFromFile ENDP

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

Ok first post done
Caché GB's 1 and 0-nly language:MASM

zedd151

Welcome to the forum!   :t

Looks like you're all set to do some 3D game programming. Congrats!

I have been lurking as a member for quite some time.  :P

Siekmanski

Creative coders use backward thinking techniques as a strategy.

felipe

Welcome to the forum! Nice intro... :icon14:

Caché GB

Thank zedd151 you for the welcome.
Yes I hear you were lurking, even
when you had no PC. Good to see
you're back.

Thank you Siekmanski for the welcome.
I have learnt a lot from your posted.
Thanks for sharing your work.

Thank you felipe for the welcome.
Worked long enough on the intro, it
was time to post.
Caché GB's 1 and 0-nly language:MASM

aw27

#5
Welcome to the Game Development subforum.
:t

Quote
As you all know the D3DX 11 utility library is deprecated thus NO

     D3DX11CreateShaderResourceViewFromFile


I think you did not put it clearly.
D3DX11CreateShaderResourceViewFromFile is in the d3d11.lib and I have been using it in my examples. Sure, Microsoft says it is deprecated for Windows 8 and above (this does not mean it does not work) and can not be used with Windows Store applications (what an inconvenience for the guys here).






daydreamer

my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

Caché GB

AW thanks for the welcome as well as the corrections.
I'll try to be better at explanations.

Also AW a special thanks for making your DMath available.

daydreamer thanks for the welcome

O.K. Take two

As you all know the D3DX 11 utility library is alive and well.

IF someone would like to know how to load a dxll ShaderResourceView as a cube map
i.e. a BOX (not a 3D TEXTURE) in MASM32 using the D3DX 11 utility library, this is how.

Note that each 1 of the 6 sides of the cube/box surface can have image staging form a
high res (.MostDetailedMip = 0) to low res (.MipLevels = 10 ). Here we are using 6, 2D
textures assembled as a cube map array housed in a .dds (Direct Draw Surface) file

The 6 images may or may not have more than one MipLevel and are stored as:-

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


;; You will need these two structures.

D3DX11_IMAGE_INFO STRUCT   16
  dWidth            DWORD  ?
  dHeight           DWORD  ?
  dDepth            DWORD  ?
  ArraySize         DWORD  ?
  MipLevels         DWORD  ?
  MiscFlags         DWORD  ?
  Format            DWORD  ?
  ResourceDimension DWORD  ?
  ImageFileFormat   DWORD  ?
D3DX11_IMAGE_INFO ENDS

D3DX11_IMAGE_LOAD_INFO STRUCT 16
  dWidth          DWORD  ?
  dHeight         DWORD  ?
  dDepth          DWORD  ?
  FirstMipLevel   DWORD  ?
  MipLevels       DWORD  ?
  Usage           DWORD  ?
  BindFlags       DWORD  ?
  CpuAccessFlags  DWORD  ?
  MiscFlags       DWORD  ?
  Format          DWORD  ?
  Filter          DWORD  ?
  MipFilter       DWORD  ?
  pSrcInfo        POINTER  ?   ; ptr to D3DX11_IMAGE_INFO
D3DX11_IMAGE_LOAD_INFO ENDS

;; and these two procedures

D3DX11GetImageInfoFromFileA PROTO STDCALL \
   pSrcFile :PTR BYTE,
   pPump :PTR ID3DX11ThreadPump,
   pSrcInfo :PTR D3DX11_IMAGE_INFO,
   pHResult :PTR HRESULT
D3DX11GetImageInfoFromFileW PROTO STDCALL \
   pSrcFile :PTR WORD,
   pPump :PTR ID3DX11ThreadPump,
   pSrcInfo :PTR D3DX11_IMAGE_INFO,
   pHResult :PTR HRESULT
IFDEF _UNICODE_
  D3DX11GetImageInfoFromFile EQU <D3DX11GetImageInfoFromFileW>
ELSE
  D3DX11GetImageInfoFromFile EQU <D3DX11GetImageInfoFromFileA>
ENDIF

D3DX11CreateTextureFromFileA PROTO STDCALL \
   pDevice :PTR ID3D11Device,
   pSrcFile :PTR BYTE,
   pLoadInfo :PTR D3DX11_IMAGE_LOAD_INFO,             
   pPump :PTR ID3DX11ThreadPump,
   ppTexture :PTR ID3D11Resource,
   pHResult :PTR HRESULT
D3DX11CreateTextureFromFileW PROTO STDCALL \
   pDevice :PTR ID3D11Device,
   pSrcFile :PTR WORD,
   pLoadInfo :PTR D3DX11_IMAGE_LOAD_INFO,             
   pPump :PTR ID3DX11ThreadPump,
   ppTexture :PTR ID3D11Resource,
   pHResult :PTR HRESULT
IFDEF _UNICODE_
  D3DX11CreateTextureFromFile EQU <D3DX11CreateTextureFromFileW>
ELSE
  D3DX11CreateTextureFromFile EQU <D3DX11CreateTextureFromFileA>
ENDIF

;; and  this macro

mAm MACRO m1:REQ, m2:REQ     ; memory to memory using accumulator i.e. EAX register
     mov eax, m2
     mov m1, eax
ENDM

.data

   ;; string of a file name to a cube .dds containing an array of 6 2d Textures

   swCubeShaderRV   dw  L(My_Cube.dds\0) ;; using the L macro by Ernest Murphy

  ;; For a ShaderResourceView used to make a sky cube or sky sphere
  ;;  (+X)_right, (-X)_left, (+Y)_up, (-Y)_down, (+Z)_front, (-Z)_back

   g_pCubeShaderResourceView  LPID3D11ShaderResourceView  null

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

.code
InitSomeScene_05 PROC

      local  ShaderResDesc:D3D11_SHADER_RESOURCE_VIEW_DESC
      local  TextureDesc:D3D11_TEXTURE2D_DESC
      local  pTextureCube:LPID3D11Texture2D
      local  loadInfo:D3DX11_IMAGE_LOAD_INFO
      local  ImageInfo:D3DX11_IMAGE_INFO

            ... other code

       invoke  RtlZeroMemory, addr loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO)
       invoke  D3DX11GetImageInfoFromFile, offset swCubeShaderRV, 0, addr ImageInfo, 0

          lea  eax, ImageInfo
          mov  loadInfo.pSrcInfo, eax
          mov  loadInfo.MiscFlags, D3D11_RESOURCE_MISC_TEXTURECUBE  ; EQU 04h
          mAm  loadInfo.dWidth, ImageInfo.dWidth
          mAm  loadInfo.dHeight, ImageInfo.dHeight
          mAm  loadInfo.dDepth, ImageInfo.dDepth
          mAm  loadInfo.MipLevels, ImageInfo.MipLevels
          mAm  loadInfo.Format, ImageInfo.Format
          mov  loadInfo.Usage, D3D11_USAGE_DEFAULT
          mov  loadInfo.BindFlags, D3D11_BIND_SHADER_RESOURCE

          mov  pTextureCube, 0

       invoke  D3DX11CreateTextureFromFile, g_pd3dDevice, offset swCubeShaderRV, addr loadInfo, 0, addr pTextureCube, 0
         test  eax, eax
           jz   @F
         push  offset szFail_CreateTextureFromFile ;; global string
          jmp  Error_Message
       @@:

     coinvoke  pTextureCube, ID3D11Texture2D, GetDesc, addr TextureDesc

          mAm  ShaderResDesc.Format, TextureDesc.Format
          mov  ShaderResDesc.ViewDimension, D3D11_SRV_DIMENSION_TEXTURECUBE
          mov  ShaderResDesc.TextureCube.MostDetailedMip, 0
          mAm  ShaderResDesc.TextureCube.MipLevels, TextureDesc.MipLevels
          mov  ShaderResDesc.TextureCube.MostDetailedMip, 0
          mov  ShaderResDesc.TextureCubeArray.First2DArrayFace, 0            ;; "zero out all
          mov  ShaderResDesc.TextureCubeArray.NumCubes, 0                    ;;  of the union."

     coinvoke  g_pd3dDevice, ID3D11Device, CreateShaderResourceView, pTextureCube, addr ShaderResDesc, addr g_pCubeShaderResourceView
          test  eax, eax
          .if (SIGN?)
               push  offset szFail_CreateShaderResource  ;; global string
                jmp  Error_Message
          .endif 

     coinvoke  pTextureCube, ID3D11Texture2D, Release

           .... more other code

           ret

InitSomeScene_05 ENDP

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

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

Siekmanski

Hi Caché GB,

Can you post the exe?
Maybe also the full source code, so we can learn from it and share our knowledge?
Creative coders use backward thinking techniques as a strategy.

Caché GB

Hi Siekmanski.

I duet you could learn from me. It is I who have learnt so much
from you and many other great members here on the forum.

To post full sauce would mean to post coding techniques
studied from everyone here in an effort to gain knowledge.

So give me time to put some Full demos together for posting that would
not be removed be the CIA (copyright infringement administration)
Caché GB's 1 and 0-nly language:MASM

felipe

Nice, nice, yes it will be great to see a full demo. I will be waiting that, thanks. :icon14:

zedd151

Quote from: Caché GB on June 11, 2018, 12:32:30 PM
Thank zedd151 you for the welcome.
Yes I hear you were lurking, even
when you had no PC. Good to see
you're back.

Do you have any idea just how difficult it is to post from a smartphone? Especially when the autocorrect changes every other word you want to type?  :P

I'm still not back full-swing yet. Doing a lot of crap cleaning on Windows 10 Home Ed. I'm almost there though,
and will be coding and making real contributions to the forum soon. See you in the Game Dev. subforum.   :biggrin:

Caché GB

zedd151, I 'am  sure your contributions to the forum will be awesome.

Quote from: zedd151
See you in the Game Dev. subforum.

I may adventure out of my "safe space" from time to time.

"subforum" yes I know -- spend way to many hours coding.

   dwHowManyHoursCoding  dw  ?

Need to get out a Bit and have a Byte.
Caché GB's 1 and 0-nly language:MASM