Shirakumo / trial

A fully-fledged Common Lisp game engine
https://shirakumo.github.io/trial
zlib License
985 stars 47 forks source link

Error: Trying to upload a region bigger than there is data in the image. while render tile-layer by tile-data #55

Closed tiwe0 closed 1 year ago

tiwe0 commented 1 year ago

Hi, I am working on a game based on tilemap. I construct a tile-data.json according to the source code. But the code complains that

Trying to upload a region bigger than there is data in the image. Region is 2400 image is 1200

here is my lisp code and tile-data.json:

lisp code:

(in-package #:org.my.project)

(setf +app-system+ "dungeon")

(define-pool dungeon-asset)

(defclass main (trial:main)
  ())

(defun launch (&rest args)
  (apply #'trial:launch 'main args))

(define-asset (dungeon-asset dungeon-tile-data) tile-data #P"path/to/tiles_data.json")

(defmethod setup-scene ((main main) scene)
  (enter (make-instance 'tile-layer :tile-data (asset 'dungeon-asset 'dungeon-tile-data)) scene)
  (enter (make-instance '3d-camera :location (vec 0 0 300)) scene)
  (enter (make-instance 'render-pass) scene)
  )

tile-data.json: json

{
  "layers": [
    {
      "data": [
        3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3,4,3
      ],
      "height": 20,
      "id": 1,
      "name": "Tile Layer 1",
      "opacity": 1,
      "type": "tilelayer",
      "visible": true,
      "width": 30,
      "x": 0,
      "y": 0
    }
  ],
  "tilesets": [
    {
      "image": "tiles_prison.png",
      "name": "dungeon-map-tilesets",
      "imagewidth": 256,
      "imageheight": 256,
      "tileheight": 16,
      "tilewidth": 16,
      "firstgid": 1,
      "columns": 16
    }
  ]
}

Anything wrong?

Shinmera commented 1 year ago

Hi, sorry, I haven't had time to look at this until now. I'm not sure what's wrong, can you try again with the latest patches and report the error?

tiwe0 commented 1 year ago

Hi, thanks for your reply!

I am using a Mac(M1). Does that matter?

Here is my test project.

dungeon_back.zip

The tilemap contains 20x30=600 tiles. For each tile, the code transforms the GID into a position (x,y). So, I understand the number 1200. But I do not understand 2400.

I am not familiar with OpenGL. Maybe I just made some stupid mistake.

Best wishes.

tiwe0 commented 1 year ago

Hi, sorry to bother you again, I just noticed the patches.

Here is the error information:

  Trying to upload a region bigger than there is data in the image.
Region is:
  2400 bytes,
  0 , 0 , 0
  30 x 20 x NIL
  RGBA RGBA
image is
  UNSIGNED-BYTE bytes
  1200 , 0 , 0
  0 x 30 x 20 @ NIL
  RGBA UNSIGNED-BYTE
tiwe0 commented 1 year ago

It seems that there is a typo in the patches on line 194 in file texture-source.lisp.

(+ off len) src-x src-y src-z src-w src-h src-d format format type
(memory-region-size region) dst-x dst-y dst-z dst-w dst-h dst-d (pixel-format texture) (pixel-type texture)))

I change it to

(+ off len) src-x src-y src-z src-w src-h src-d format type
(memory-region-size region) dst-x dst-y dst-z dst-w dst-h dst-d (pixel-format texture) format (pixel-type texture)))

Here is the error informathion again:

  Trying to upload a region bigger than there is data in the image.
Region is:
  2400 bytes,
  0 , 0 , 0
  30 x 20 x NIL
  RGBA UNSIGNED-BYTE
image is
  1200 bytes
  0 , 0 , 0
  30 x 20 x NIL @ RGBA
  RGBA UNSIGNED-BYTE

with the debuger, I found more information in upload-texture-source:

  1. source:
    
    (inspect trial::source)

The object is a STRUCTURE-OBJECT of type TRIAL:TEXTURE-SOURCE.

  1. PIXEL-DATA: #(2 15 3 15 2 15 3 15 2 15 3 15 ...) <- with length of 1200

  2. PIXEL-TYPE: :UNSIGNED-BYTE

  3. PIXEL-FORMAT: :RGBA

  4. TARGET: NIL

  5. LEVEL: NIL

  6. SRC: (NIL NIL NIL NIL NIL NIL)

  7. DST: (NIL NIL NIL NIL NIL NIL)

  8. texture

    
    (inspect trial:texture)

The object is a STANDARD-OBJECT of type TRIAL:TILEMAP.

  1. GENERATOR: #<TRIAL:TILE-DATA DUNGEON-ASSET/DUNGEON-TILE-DATA>
  2. NAME: T
  3. DEPENDENCIES: NIL
  4. DATA-POINTER: NIL
  5. WIDTH: 30
  6. HEIGHT: 20
  7. DEPTH: "unbound"
  8. TARGET: :TEXTURE-2D
  9. LEVELS: "unbound"
  10. SAMPLES: "unbound"
  11. INTERNAL-FORMAT: :RG8UI
  12. SOURCES: (#)
  13. MAG-FILTER: :NEAREST
  14. MIN-FILTER: :NEAREST
  15. MIPMAP-LEVELS: "unbound"
  16. MIPMAP-LOD: "unbound"
  17. ANISOTROPY: "unbound"
  18. WRAPPING: "unbound"
  19. BORDER-COLOR: "unbound"
  20. SWIZZLE: "unbound"
  21. STORAGE: "unbound"
  22. PIXEL-TYPE: :UNSIGNED-BYTE
  23. PIXEL-FORMAT: :RG-INTEGER
  24. TILESET: #<TRIAL:TILESET 256x256 RGBA8 ALLOCATED {70099B9553}>

It seems that

for texture, len(2400) = stride(4channel) x width(30) x height(20); for source, the pixel-data is an array containing positions of each tile(1200).

Give me several days, I may figure out the solution.

Shinmera commented 1 year ago

df3056e7 should fix your issue.

tiwe0 commented 1 year ago

Hi, thanks for replying!

I applied the change, but a new error was raised:

#<THREAD "main thread" RUNNING {7006360723}>:
  OpenGL signaled (1282 . INVALID-OPERATION) from TEX-SUB-IMAGE-2D.

So I reviewed the code that loads the tilemap using "tilemap.dat".

It seems that there is a typo in the patches on line 55 in the file tile-data.lisp:

:sources (list (make-image-source pixel-data width height :unsigned-byte :rg))

I changed it to:

:sources (list (make-image-source pixel-data width height :unsigned-byte :rg-integer))

And finally, it worked!

Thank you!