Jump to content
HardXOR

Optimize/speedup code for texture decoding - (Moved)

Recommended Posts

Hello AutoIt community

I run into speed problem in my script wich i cant solve myself, problem is with decoding texture loop - for better explanation, you need extract from file pallete (16x 16 RGB color) and picture data (224 * 128 byte), then use correct color for your picture data.... nothing extra hard and also texture is quite small 224*256

it is for my car model viewer/later maybe editor GranTurismo 2 from Playstation 1, so its old dataformat and i cant understand why AutoIt take so long to decode texture when good old Playstation almost 2,5 decades old can do that nearly immediately (when you list through cars in shop or garage)

My first atempt was create all trought dllstructure, because its easier approach, but it was soooo slow (40-50s for create textures) then i upgrade my routine via arrays, first 3D arrays later only 1D, next i put decoding colors outside loop but it is still not enough, my last version took cca 15s wich is still unacceptable for car model viewer when you click on one carmodel from listview (1100 cars for whole game) and you must wait 15-16s for model to load.... :( oh and i forgot mention some cars have more then 1 color (much more... 8-9-10 etc) soloading take 8-9-10 times more time

in attachment i post texture file from GranTurismo 2 for one car (contain only 1 color) and also my dll struct version and array version code

dll struct version - ± 40 sec (33 without saving)

#include <FileConstants.au3>

Global $IMDT[256][256][4]

LoadTexture("ufs9r.cdp")

Func LoadTexture($file)
    $fileHandle = FileOpen($file, $FO_BINARY)

    $header = FileRead($fileHandle, 0x20)
    ConsoleWrite("header> " & $header & @CRLF)
    $PAL = FileRead($fileHandle, 0x200)
    ConsoleWrite("PAL> " & $PAL & @CRLF)
    FileSetPos($fileHandle, 0x43A0, $FILE_BEGIN)
    $IMD = FileRead($fileHandle, 0x7000)
    ConsoleWrite("IMD> " & $IMD & @CRLF)

    $st = DllStructCreate("BYTE[512]")
    DllStructSetData($st, 1, $PAL)
    $struct_PAL = DllStructCreate("WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16];WORD[16]", DllStructGetPtr($st))

    $struct_IMD = DllStructCreate("BYTE[" & 0x7000 & "]")
    DllStructSetData($struct_IMD, 1, $IMD)


    $start = TimerInit()
    For $i = 0 To 15
        For $j = 0 To 223
            $cn = 0
            For $k = 0 To 127
                $bt = DllStructGetData($struct_IMD, 1, $j * 128 + $k + 1)

                $blue = BitShift(DllStructGetData($struct_PAL, $i + 1, BitAND($bt, 0x0F) + 1), 7)
                $IMDT[$j][$cn][0] = $blue
                $green = BitShift(DllStructGetData($struct_PAL, $i + 1, BitAND($bt, 0x0F) + 1), 2)
                $IMDT[$j][$cn][1] = $green
                $red = BitShift(DllStructGetData($struct_PAL, $i + 1, BitAND($bt, 0x0F) + 1), - 3)
                $IMDT[$j][$cn][2] = $red

                If DllStructGetData($struct_PAL, $i + 1, BitAND($bt, 0x0F) + 1) = 0 Then
                    $IMDT[$j][$cn][3] = 0x00
                Else
                    $IMDT[$j][$cn][3] = 0xFF
                EndIf

                $cn += 1

                $blue = BitShift(DllStructGetData($struct_PAL, $i + 1, BitShift($bt, 4) + 1), 7)
                $IMDT[$j][$cn][0] = $blue
                $green = BitShift(DllStructGetData($struct_PAL, $i + 1, BitShift($bt, 4) + 1), 2)
                $IMDT[$j][$cn][1] = $green
                $red = BitAND(BitShift(DllStructGetData($struct_PAL, $i + 1, BitShift($bt, 4) + 1), - 3), 0xFF)
                $IMDT[$j][$cn][2] = $red

                If DllStructGetData($struct_PAL, $i + 1, BitShift($bt, 4) + 1) = 0 Then
                    $IMDT[$j][$cn][3] = 0x00
                Else
                    $IMDT[$j][$cn][3] = 0xFF
                EndIf

                $cn += 1
            Next
        Next
        saveTGA($i)
    Next
    ConsoleWrite("t " & TimerDiff($start) & @CRLF) ; +- 40 seconds

    FileClose($fileHandle)
EndFunc

Func saveTGA($name)
    Local $tga[18] = [0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x20, 0x20]
    Local $data
    for $i = 0 To 17
        $data &= Hex($tga[$i], 2)
    Next
    For $i = 0 To 255
        For $j = 0 To 255
            For $k = 0 To 3
                $data &= hex($IMDT[$i][$j][$k], 2)
            Next
        Next
    Next
    $binary = FileOpen("test\" & $name & ".tga", BitOR($FO_BINARY, $FO_OVERWRITE, $FO_CREATEPATH))
    FileWrite($binary, "0x" & $data)
    FileClose($binary)
EndFunc

 

array version - ± 15 sec (under 10s without saving)

#include <FileConstants.au3>

LoadTexture2("ufs9r.cdp")

Func LoadTexture2($file)
    $fileHandle = FileOpen($file, $FO_BINARY)

$a = TimerInit()
    Global $header[0x20]
    For $i = 0 To UBound($header) - 1
        $header[$i] = Int(String(FileRead($fileHandle, 1))) ; read 0x20 bytes
    Next
ConsoleWrite("header " & TimerDiff($a) & @CRLF)

$a = TimerInit()
    Global $PAL[0x100]
    For $i = 0 To UBound($PAL) - 1
        $PAL[$i] = Number(FileRead($fileHandle, 2)) ; read 0x200 (16*16) words
    Next

    Global $PALcolor[16 * 16 * 4]
    For $i = 0 To UBound($PAL) - 1
        $PALcolor[$i * 4 + 0] = BitShift($PAL[$i], 7)
        $PALcolor[$i * 4 + 1] = BitShift($PAL[$i], 2)
        $PALcolor[$i * 4 + 2] = BitShift($PAL[$i], -3)
        If $PAL[$i] = 0 Then
            $PALcolor[$i * 4 + 3] = 0x00
        Else
            $PALcolor[$i * 4 + 3] = 0xFF
        EndIf
    Next


ConsoleWrite("PAL " & TimerDiff($a) & @CRLF)

$a = TimerInit()
    FileSetPos($fileHandle, 0x43A0, $FILE_BEGIN)
    Global $IMD[0x7000]
    For $i = 0 To UBound($IMD) - 1
        $IMD[$i] = Int(String(FileRead($fileHandle, 1))) ; read 0x7000 bytes
    Next
ConsoleWrite("IMD " & TimerDiff($a) & @CRLF)

Global $IMDT[256*256*4]

$a = TimerInit()
    For $i = 0 To 15
        For $j = 0 To 223
            $cn = 0
            For $k = 0 To 127
                $byte = $IMD[$j * 128 + $k] ; byte for decode

                $index = $j * 1024 + $cn * 4
                $index2 = $i * 0x40 + BitAND($byte, 0x0F) * 4
                $IMDT[$index + 0] = $PALcolor[$index2 + 0] ; blue
                $IMDT[$index + 1] = $PALcolor[$index2 + 1] ; green
                $IMDT[$index + 2] = $PALcolor[$index2 + 2] ; red
                $IMDT[$index + 3] = $PALcolor[$index2 + 3] ; alpha
                $cn += 1

                $index = $j * 1024 + $cn * 4
                $index2 = $i * 0x40 + BitShift($byte, 4) * 4
                $IMDT[$index + 0] = $PALcolor[$index2 + 0] ; blue
                $IMDT[$index + 1] = $PALcolor[$index2 + 1] ; green
                $IMDT[$index + 2] = $PALcolor[$index2 + 2] ; red
                $IMDT[$index + 3] = $PALcolor[$index2 + 3] ; alpha
                $cn += 1
            Next
        Next
;~      $b = TimerInit()
        saveTGA2($i)
;~      ConsoleWrite("save TGA " & TimerDiff($b) & @CRLF)
    Next

ConsoleWrite("full time " & TimerDiff($a) & @CRLF) ; 16 seconds


    FileClose($fileHandle)
EndFunc

Func saveTGA2($name)
    Local $tga[18] = [0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x20, 0x20]
    Local $data

    For $i = 0 To 17
        $data &= Hex($tga[$i], 2)
    Next
    For $i = 0 To UBound($IMDT) - 1
        $data &= Hex($IMDT[$i], 2)
    Next

    $binary = FileOpen("test\" & $name & ".tga", BitOR($FO_BINARY, $FO_OVERWRITE, $FO_CREATEPATH))
    FileWrite($binary, "0x" & $data)
    FileClose($binary)
EndFunc

if anyone can optimize my code I would be very grateful, or pointing me to better solution, thx

ufs9r.cdp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • By sksbir
      Hi
      Trying this from autoit v3.3.14.5 and SCITE 3.19.102.1901.0 :
      help file , page "Language Reference - Variables" , sample autoit script about maps:

      Maps must be declared before use by defining their scope using the 'Global/Local/Static' keywords. Local $mControls[]
      So is my test script : only with this local declation.
      -check syntax is OK
      - running script : 

      test.au3" (13) : ==> Variable subscript badly formatted.: Local $mControls[] Local $mControls[^ ERROR ->14:51:49 AutoIt3.exe ended.rc:1
      any clue ?
    • By Blitzkid
      Hello, i want to search several directories for files with the largest numbers behind them (Like "video123") . They dont have a datatype. But there are also files with longer names and datatypes in these folders (Like "video778.mp4"). Is it possible to filter the _FileListToArray Syntax from
      to smth. like
       
      Here is my Code
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> #include <File.au3> $filedir = @ScriptDir & "\" _checkfile() Func _checkfile() ConsoleWrite("______________________" & @CRLF) Local $arr[3] = ["music", "picture", "video"] For $i = 0 To UBound($arr) - 1 Local $arrayfiles = _FileListToArray($filedir & $arr[$i], $arr[$i] & "*", 1) If @error = 1 Then ConsoleWrite($arr[$i] & "Error 1") EndIf If @error = 4 Then ConsoleWrite($arr[$i] & "Error 2") ;Exit EndIf $arrayfilter = _ArrayMax($arrayfiles, 0, 1) Global $stringfiles = StringReplace($arrayfilter, $arr[$i], "") ConsoleWrite($arrayfilter & @CRLF) Next EndFunc ;==>_checkfile  
    • By jmp
      Script running good but error in line 7.
      When i run this script :
      #include <IE.au3> #include <Array.au3> $oIE = _IEAttach ("Shop") $oTable = _IETableGetCollection ($oIE, 1) $aTableData = _IETableWriteToArray ($oTable) For $inumber = 1 To UBound($aTableData) -1 $table = $aTableData[4][$inumber] MsgBox(0, "", $table) Next I got Error: array variable has incorrect number of subscripts or subscript dimension range exceeded
    • By nacerbaaziz
      Good morning guys, i hope that you're all well.
      guys, i have a problem and i hope that you can help me
      i've created an 3d array
      the array Contain a Categories info
      as folow
      $array[n][0][0] = Categorie name
      $array[n][0][1] = Categorie file path
      $array[n][0][2] = Categorie contents number
      $array[n][m][0] = link name
      $array[n][m][1] = link url
      $array[n][m][2] = link section name
      in my tool i want to add an option to delete a Category
      as you know the Categorie mean that must delete a region from the array
      when i tried to use _arrayDelete
      with the 2d array it work well
      but here i couldn't find any way to do that, can any one help me please?
      thanks in advance.
    • By nacerbaaziz
      hello sirs, please help me
      i tried to create a function that read a folder files to 3d array
      e.g
      $array[n][0][0] = ctName
      $array[n][0][1] = ctFilePath
      $array[n][0][2] = crtsections number
      $array[n][m][0] = KeyName
      $array[n][m][1] = KeyVal
      $array[n][m][2] = keySectionName
       
      that the array
      when i put one file into the folder all things work fine
      but when i put more than one file
      the last file worked fine but the others only the first key is showing
      please can you help me to correct this problem
      here is the example with the folder
      please accept my greetings
      and thanks in advance
       
      array3d.zip
×
×
  • Create New...