About Store Forum Documentation Contact



Post Reply 
[SOLVED] Plain cache objects creation
Author Message
Hokan Offline
Member

Post: #1
[SOLVED] Plain cache objects creation
Hi everyone,

Can't get how exactly cache works. Is it just map of shared pointers, wrapped in own methods and classes, or something more?

I was trying to implement moon phases, so I planned to:
  1. Create empty image with desired mode and size
  2. Fill image with certain data
  3. Set created image as moon's image

Code:
ImagePtr originalImage = UID(...);
ImagePtr maskedImage = UID(...);
auto height = originalImage()->h();
auto width = originalImage()->w();
auto depth = originalImage()->d();

// maskedMoonImage is ImagePtr class instance                
maskedMoonImage = new Image(width, height, depth, IMAGE_R8G8B8A8, IMAGE_2D);

originalImage()->lockRead();
maskedImage()->lockRead();        
maskedMoonImage()->lock(LOCK_WRITE);

// some operations

originalImage()->unlock();
maskedImage()->unlock();
maskedMoonImage()->unlock();

originalImage = NULL;
maskedImage = NULL;

// moon points exactly at object in Astro container, no doubt
moon->image = maskedMoonImage;

And I stuck on 3rd step.
I'm sure that "maskedMoonImage" filled with correct data - data can be exported to png file perfectly, but in application moon not renders. On each frame moon->image points to correct data, but engine shows no image.

Moreover, Images.contains(maskedMoonImage); call returns false.
On the other hand, if I create maskedMoonImage as Image class instance and fill it with data, everything works fine, but setting moon->image = maskedMoonImage; seems to be impossible - cache know nothing about Image object and I can't retrieve pointer CacheElementPointer to maskedMoonImage, or convert C++ pointer to maskedMoonImage to CacheElementPointer.

So my questions are:
  1. Does EE cache mapped directly to files and can't store plain data?
  2. If so, how can I bypass it and correctly use cache with non-file data?
(This post was last modified: 08-22-2015 04:03 PM by Hokan.)
08-17-2015 08:33 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #2
RE: Plain cache objects creation
I recommend avoiding 'new'.
Use a variable "Image image".
and if you want to use ImagePtr then do "ImagePtr image_ptr=ℑ"
08-18-2015 07:32 AM
Find all posts by this user Quote this message in a reply
Hokan Offline
Member

Post: #3
RE: Plain cache objects creation
Thanks for answer, but this did not help.
And this is seems right - heap pointer from "new" call doesn't differ from pointer to stack Image variable.

Images.contains(&maskedMoonImage); call still returns false, so as Images.contains(moon->image);, where moon->image already points to maskedMoonImage (no difference, I know, just double check)
Assignment operator seems work fine - it triggers CACHE._incRef(T._data= data);, but cache doesn't hold any reference to loaded data and Images.ptrCount(moon->image); call returns -1
08-18-2015 05:46 PM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #4
RE: Plain cache objects creation
Are you sure that the data is correctly written to the image? have you tried doing maskedMoonImage->ExportPng(); to see that it actually has data written to it?

Personally I would not use ImagePtr for a dynamically written image and instead store it with Image imagemask, then wherever you have to pass ImagePtr you pass &imagemask;
(This post was last modified: 08-18-2015 06:57 PM by Zervox.)
08-18-2015 06:55 PM
Find all posts by this user Quote this message in a reply
Hokan Offline
Member

Post: #5
RE: Plain cache objects creation
(08-17-2015 08:33 PM)Hokan Wrote:  I'm sure that "maskedMoonImage" filled with correct data - data can be exported to png file perfectly, but in application moon not renders.

Double checked this right now - works fine.

(08-18-2015 06:55 PM)Zervox Wrote:  Personally I would not use ImagePtr for a dynamically written image and instead store it with Image imagemask, then wherever you have to pass ImagePtr you pass &imagemask;

Doesn't work.
(This post was last modified: 08-18-2015 07:00 PM by Hokan.)
08-18-2015 06:58 PM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #6
RE: Plain cache objects creation
I guess it still returns Image_2D as its mode as well?
I remember having some similar problem with this earlier when it came to drawing image but can't remember what it was.
could you try using lockRead() before drawing it?
08-18-2015 07:03 PM
Find all posts by this user Quote this message in a reply
Hokan Offline
Member

Post: #7
RE: Plain cache objects creation
Yes, image's mode is IMAGE_2D. I've even thought colour format is the reason and tried BRG, DXT_1, DXT_3 and DXT_5 along with RGB - nothing helped.

Tried locking and unlocking each frame and lockRead() once after creation - no effect.
08-18-2015 07:12 PM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #8
RE: Plain cache objects creation
You probably checked this as well, but if not could you try checking Alpha values?
(This post was last modified: 08-18-2015 07:26 PM by Zervox.)
08-18-2015 07:25 PM
Find all posts by this user Quote this message in a reply
Hokan Offline
Member

Post: #9
RE: Plain cache objects creation
Yeah, was third or fourth thought came to my mind - even if all pixels have 1.0 alpha value, image doesn't renders.
08-18-2015 07:48 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #10
RE: Plain cache objects creation
If you use "new Image" then you're allocating the memory, not Images. so Images does not contain that image.
08-18-2015 10:26 PM
Find all posts by this user Quote this message in a reply
Hokan Offline
Member

Post: #11
RE: Plain cache objects creation
Sure, but when I use Images' assignment operator, doesn't cache should check if he has data, and, if not, copy it and set reference count to 1?
(This post was last modified: 08-19-2015 08:23 AM by Hokan.)
08-19-2015 06:48 AM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #12
RE: Plain cache objects creation
reference counting via ImagePtr will be enabled only if the Image is stored inside Images cache
08-19-2015 08:37 AM
Find all posts by this user Quote this message in a reply
Hokan Offline
Member

Post: #13
RE: Plain cache objects creation
According to cache element assignment operator signature, it has 2 overloaded implementations. One of each is CacheElmPtr& operator=( TYPE *data);, so why can't I pass pointer to Image instance created on heap?
In such way ImagePtr should copy data (so as you say, Images will know about new data), via pointer from "new Image" and set reference count to 1.
That what I expect from shared pointer.

Either way, creating Image class instance and using it as pointer to cache element via ImagePtr pointer = &imageInstance, doesn't work - Images not contain imageInstance's data.
08-19-2015 11:01 AM
Find all posts by this user Quote this message in a reply
Hokan Offline
Member

Post: #14
RE: Plain cache objects creation
UPDATE:

  1. If I create image via
    Code:
    auto width = originalImage()->w();
    auto height = originalImage()->h();        
                    
    maskedMoonImage.create2D(width, height, originalImage()->type());
    then perform same operations, then export to png, I get image, with all pixels equal to (0, 0, 0, 0)

    EDIT:
    My bad, originalImage has IMAGE_DXT3 format, no surprise maskedMoonImage doesn't fill with correct data, as I manipulate it like RGB image
  2. If I copy data from original image (originalImage()->copy(maskedMoonImage), then immediately originalImage = NULL), then change masked image pixels, engine successfully draw maskedMoonImage AS originalImage.
    Exported maskedMoonImage also looks exactly like originalImage

    NOTE:
    After
    Code:
    originalImage()->copy(maskedMoonImage);
    originalImage = NULL

    Images.ptrCount call over moon->image, moon->image(), originalImage, &maskedMoonImage returns -1. Why engine suddenly be able to draw maskedMoonImage I don't know.

    EDIT:
    If, I copy data, converting it to RGB format - originalImage()->copy(maskedMoonImage, -1, -1, -1, IMAGE_R8G8B8A8), exported maskedMoonImage looks right, but engine still renders maskedMoonImage as originalImage
(This post was last modified: 08-22-2015 03:23 PM by Hokan.)
08-22-2015 02:54 PM
Find all posts by this user Quote this message in a reply
Hokan Offline
Member

Post: #15
RE: Plain cache objects creation
SOLUTION:

Finally figured, what was wrong:

  1. Use Image objects
  2. To get ImagePtr from Image, just call pointer operator:
    imagePtrObjectInstance = &imageObjectInstance
  3. Create Image object, and explicitly set mipmap amount:
    maskedMoonImage.create2D(width, height, IMAGE_R8G8B8A8, 1);
  4. Use blend = true on Astro objects, if you want to make alpha-channel changes visualised
(This post was last modified: 08-22-2015 04:07 PM by Hokan.)
08-22-2015 04:03 PM
Find all posts by this user Quote this message in a reply
Post Reply