Jump to content
Sign in to follow this  
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Zaoka
      Hi guys
      Need little help with filtering.
      I'm trying to filter specific weeks from power pivot table using this code recorded with Excel VBA :
       
      #include <Excel.au3> Global $oExcel = _Excel_Open() Global $oWorkbook = _Excel_BookOpen($oExcel, "C:\Users\....\Orders.xlsb") $oWorkbook.PivotTables("PivotTable1").PivotFields( _ "[Report 2].[Week].[Week]").VisibleItemsList = Array( _ "[Report 2].[Week].&[10]", "[Report 2].[Week].&[11]", _ "[Report 2].[Week].&[12]", "[Report 2].[Week].&[13]", _ "[Report 2].[Week].&[14]", "[Report 2].[Week].&[15]") But get error
      error: Array(): undefined function. Not sure how to resolve this.
    • By LxP
      I'm trying to pass a nested array to a function, such that the function alters the inner array.
      I was surprised to find that this minimal reproducible example, despite its use of ByRef, seems to pass a copy of the inner array to the function:
      #include <Array.au3> ; a boring old array Local $aInnerArray[5] = [1, 2, 3, 4, 5] ; a one-element array containing a reference to the other array Local $aOuterArray[1] = [$aInnerArray] ; intention: take a nested array and alter its inner array ; reality: the inner array seems to be getting copied Func ChangeIt(ByRef $aOuter) Local $aInner = $aOuter[0] $aInner[2] = 0 EndFunc ; Expected: [1, 2, 3, 4, 5] ; Actual: [1, 2, 3, 4, 5] ✔ _ArrayDisplay($aInnerArray, 'Before') ; $aOuterArray passed by-ref, should receive reference to $aInnerArray ; Therefore should change $aInnerArray to [1, 2, 0, 4, 5] ChangeIt($aOuterArray) ; Expected: [1, 2, 0, 4, 5] ; Actual: [1, 2, 3, 4, 5] ✘ _ArrayDisplay($aInnerArray, 'After') I suspect that either:
      the copy is taking place in the first line of the function (I couldn't find a way to access the inner array without first assigning it to a variable though); or ByRef doesn't propagate into inner levels of the data structure being passed, which seems less likely to me. Could someone please point me in the right direction to get this working as intended?
      Update: the answer
      ; WRONG: ; a one-element array containing a reference to the other array Local $aOuterArray[1] = [$aInnerArray] The assumption I made about this code is wrong—it actually copies $aInnerArray into $aOuterArray, so there are now two unrelated $aInnerArray instances.  It is not possible to store arrays in other arrays by reference.
      If it is necessary to refer to a mutable array in multiple places, consider holding it in a global variable.  Where a collection of mutable arrays needs to be accessed in multiple places (as in my case), consider storing them in a global array and referring to each sub-array by index (also known as the Registry pattern).
    • By VinMe
      HELLO All,
      is there any function in Auto it to Remove the repeating  strings separated by ","  which is present in the array.
      ex.
      Available state
      a[0]= D97,D96,,D85,D86,D85,D86,D85,D86,D85,,D86,D85,D86,D85,D86
      a[1]=D85,D24,,,,,D85
      Required state
      a[0]= D97,D96,D85,D86,
      a[1]=D85,D24
      thank  you in advance!
      vin!
    • By D2thunder
      Hi all, I wasnt active in this Forum forum for Years. 😅
      Since I need something with Json in AutoIt i write a little UDF to save a variable to file to read it with another language - and back. After a while until my other script grows i thought - lets integrate all variables wich is possible, because i want have arrays to.
      This is my first public UDF in Autoit - normaly my AutoIt scripts are quick an dirty.... - like the examples for this udf. - someone want to write nice examples?
      I tried to write this UDF properly, cared for processing speed, low mem consumption as possible, a short code, and a high usability without beeing very familiar with arrays.
      What is it for? - convert nearly all types of variables to a json string for saving it in a file or to an ini. Read it back in to a same formated variable and preserv variable types. All array types are supported with nested arrays, the only restriction is ram and array/stringsize - and time if the array is very big. You can use Filters for variable types and some other options.
      If anyone found a bug or any suggestions, please post it.
       
      get Json.au3 from here Json.au3
       
       
      JsonVar 2019.01.25.1.zip
    • By Colduction
      Hi, i have a source code of "Danny35d" and i have 2 main question that are:
       How to extract this function's results (Datas are as Array) to combo-box without set number of datas?  When i type 1, 2 in _SystemUsers($AccountType = 0) to get only Local or Domain users, it just give me both of them, it's old problem of this function, please share us debugged code :)❤ #include <Array.au3> $Users = _ArrayToString(_SystemUsers(), "|", 1) ConsoleWrite($Users & @CRLF) #cs =============================================================================== Function: _SystemUsers($AccountType = 0) Description: Return an array with the local or domain username Parameter(s): $AccountType: Local, domain or both username 0 = Local and Domain usernames 1 = Local usernames only 2 = Domain usernames only Returns: An array with the list of usernames - Succeeded @error 1 - Didn't query any username @error 2 - Failed to create Win32_SystemUsers object @error 3 - Invalid $AccountType Author(s): Danny35d #ce =============================================================================== Func _SystemUsers($AccountType = 0) Local $aSystemUsers Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20 Local $colItems = "", $strComputer = "localhost" If Not StringRegExp($AccountType, '[012]') Then Return SetError(3, 3, '') $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemUsers", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If IsObj($colItems) Then For $objItem In $colItems $Output = StringSplit($objItem.PartComponent, ',') If IsArray($Output) Then $Temp = StringReplace(StringTrimLeft($Output[2], StringInStr($Output[2], '=', 0, -1)), '"', '') If $AccountType = 0 Or ($AccountType = 1 And @ComputerName = $Temp) Then $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|' ElseIf $AccountType = 2 And @ComputerName <> $Temp Then $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|' EndIf EndIf Next $aSystemUsers = StringTrimRight($aSystemUsers, 1) If $aSystemUsers = '' Then Return(SetError(1, 1, $aSystemUsers)) Return(SetError(0, 0, StringSplit($aSystemUsers, '|'))) Else $aSystemUsers = '' Return(SetError(2, 2, $aSystemUsers)) EndIf EndFunc ;==>_SystemUsers Thanks to your best Team.
×
×
  • Create New...