LWJGL Forum

Please login or register.

Login with username, password and session length
Pages: 1 2 [3] 4 5 ... 10
 21 
 on: April 23, 2017, 06:20:22 
Started by bcbradle - Last post by spasi
The VkClearColorValue class does not decide anything. Like all struct classes, it's a wrapper over a pointer address + byte size. Because it's a union, the offset of all 3 members is zero and the byte size is 4 x 4 bytes (i.e. 16 bytes total, not 48).

How those bytes are interpreted is specified by Vulkan; it depends on the format of the image or attachment. The getters and setters with different types provide type-safety over the raw bytes. You can use those that match the format of the image or attachment that you need to clear to a specific color. Using value.float32(2, 3f) and value.int32(2, 3) will both write 4 bytes at offset 8 in the struct, but obviously different bytes will be written. There is no difference between int32 and uint32 in Java, but because of the properties of two's complement binary representation, you can use a Java int as a uint32_t.

 22 
 on: April 23, 2017, 04:16:18 
Started by Spytrycer - Last post by Spytrycer
Thank you alot!
I understand my problem now way more.

I implemented the Code into mine and changed it a little bit, because i didnt like the Entity to move continuously and faster and faster when accelerated, or i implemented the code wrong at first :).

But i did keep the integration of differentials with delta that solves the Rotation problem i had, thanks alot for that!

Im now struggeling with the Coordinates of an Entity.
I want an Entity to have specified coordinates in the World, that i can refer to and change in order to chase it with the Camera(which right now doesnt work) and to set to a specific value if nessesary.

Here is my implementation, maybe you can help me getting to a solution and look over my Code to see if i implemented correctly:

(And Yes, i know casting a Double into a Float is horribly inaccurate and can be terrible, i intend to go over these castings later on, now i want to get this to work)

Code: [Select]
public void move(double delta)
{
float dt = (float)delta;

//linearVel.fma(dt, linearAcc);

    //angularVel.fma(dt, angularAcc);

    this.position.x += dt*linearVel.x;
    this.position.y += dt*linearVel.y;
    this.position.z += dt*linearVel.z;

    transformationMatrix.rotateX(dt*angularVel.x)
          .rotateY(dt*angularVel.y)
          .rotateZ(dt*angularVel.z);
   
    transformationMatrix.translate( dt*linearVel.x, dt*linearVel.y, dt*linearVel.z);
        }

Thats the changed Move Method. Im now not calculation in it, instead im just changing the transformationMatrix which i have made part of the Entity Class in general, terrible solution, but right now i want to get it to work.

//CAMERA MOVE
Code: [Select]
public void move(double delta)
{


float dt = (float)delta;

    //linearVel.fma(dt, linearAcc);

    //angularVel.fma(dt, angularAcc);

    this.angularVel.x = player.angularVel.x;
    this.angularVel.y = -player.angularVel.y;
    this.angularVel.z = player.angularVel.z;

    this.pitch += dt*angularVel.x;
    this.yaw += dt*angularVel.y;
    this.roll += dt*angularVel.z;
   

   
    linearVel.x = player.linearVel.x;
    linearVel.y = player.linearVel.y;
    linearVel.z = player.linearVel.z;
   
    position.x += dt*linearVel.x;
    position.y += dt*linearVel.y;
    position.z += dt*linearVel.z;

    viewMatrix
    .rotateLocalX((float)Math.toRadians(dt*angularVel.x))
            .rotateLocalY((float)Math.toRadians(dt*angularVel.y))
            .rotateLocalZ((float)Math.toRadians(dt*angularVel.z))
    .translateLocal(-dt*linearVel.x, -dt*linearVel.y, -dt*linearVel.z);
}



The Camera is the Problem.
I want it to tag along the Player, i try that by giving it the players linearVelocity.
The Camera does take this and with my code the Position changes to, but the Cameras position doesnt, or at least not really. It is kind of vibrating in place.
I tried to set the translation to:
Code: [Select]
.translateLocal(1,1,1);and it turns out that it DOES move it that way, but it seems to be returned to its original position everytime.

I also spawned in a referenceEntity that im not moving and that proves this theory. The Camera is trying to move but it seems like it Cant for some reason.
What am i doing wrong??







 23 
 on: April 23, 2017, 03:18:22 
Started by bcbradle - Last post by bcbradle
The vulkan specification states that VkClearColorValue is a union type between float, int, and unsigned int arrays.

Java doesn't have a union type so it can't expose that in the api. The lwjgl VkClearColorValue class offers getters and setters for each of float, int and unsigned int arrays.

How does this class decide which field to use (which field is used natively) if they have all been set?

 24 
 on: April 22, 2017, 22:04:29 
Started by bcbradle - Last post by spasi
Would it be possible to do something with SoftReference and finalize()-methods to somehow make memory leaks less likely? A kind of safety net?

The safety net already exists; use memory allocated via ByteBuffer.allocateDirect. Deallocation is already handled automatically with a reference queue. Though, you sacrifice EA (the buffer reference escapes by definition) and can't be sure when or even if the buffer has been freed (2 GC cycles minimum for it to happen).

I follow these rules and never had serious issues:

- Use native memory as little as possible.
- When you do need it, use the MemoryStack for small, short-lived allocations.
- If the MemoryStack is not applicable, use explicit malloc/free if cleanup is straightforward.
- If not, use BufferUtils (i.e. ByteBuffer.allocateDirect).

 25 
 on: April 22, 2017, 21:24:14 
Started by bcbradle - Last post by Cornix
You must carefully track every memory allocation in your program. This is tedious and error-prone and Java's type system does not help with making it easier. But, you need to keep in mind that it's no different in C (and LWJGL tries to "emulate" writing C in Java). Even in C, when you have a random pointer to a struct, you don't know if it should be freed or not without additional information about what the program is doing. You don't know if it's allocated by you, by another library, or even if it's allocated on the heap or the stack. It's the same thing in LWJGL. You must think in terms of memory allocations; who allocated it and owns them memory, by what means (malloc vs ByteBuffer.allocateDirect vs MemoryStack), who else has live pointers to that memory. The nice thing in Java* is that it's easier to hide the complexity in friendly abstractions and to debug problems when they occur (imho).
Would it be possible to do something with SoftReference and finalize()-methods to somehow make memory leaks less likely? A kind of safety net?

 26 
 on: April 22, 2017, 19:08:46 
Started by bcbradle - Last post by spasi
Yes, every struct class has a corresponding Buffer class.

 27 
 on: April 22, 2017, 19:05:44 
Started by bcbradle - Last post by spasi
Random thoughts:

Instances of struct classes and struct buffer classes are both simple wrappers over a pointer address + byte size. In the case of structs, the byte size is sizeof(<struct type>), whereas for struct buffers it's N * sizeof(<struct type>).

In both cases, there's only one Java object (if not eliminated by escape analysis). A struct buffer does not pre-allocate struct instances at each offset, there's only one Java instance and a contiguous block of off-heap memory. While iterating over a struct buffer you may allocate temporary struct instances (e.g. using get()), but those will hopefully be eliminated via EA.

You can iterate over a struct buffer using the flyweight pattern, if you don't trust EA (or profiling has shown that EA doesn't do a good job at a particular hot method):

Code: [Select]
// This:
VkCommandPoolCreateInfo.Buffer foo = VkCommandPoolCreateInfo.malloc(2);
foo.get(1).queueFamilyIndex(0);

// is equivalent to this:
VkCommandPoolCreateInfo.Buffer foo = VkCommandPoolCreateInfo.malloc(2);
foo.position(1);
foo.queueFamilyIndex(0);

When you put() a struct on a struct buffer, you are not copying a pointer address; you're doing a memcpy of the struct's data to the buffer at the specified offset. Changes to the struct will NOT be reflected to the struct buffer, and vice versa.

When you get() from a struct buffer, you're creating a new struct wrapper, pointing at the memory address at the specified offset. Changes to the new wrapper will BE reflected in the original struct buffer and vice versa. The struct instance in this case should not be freed of course, because the original struct buffer owns its memory.

You must carefully track every memory allocation in your program. This is tedious and error-prone and Java's type system does not help with making it easier. But, you need to keep in mind that it's no different in C (and LWJGL tries to "emulate" writing C in Java). Even in C, when you have a random pointer to a struct, you don't know if it should be freed or not without additional information about what the program is doing. You don't know if it's allocated by you, by another library, or even if it's allocated on the heap or the stack. It's the same thing in LWJGL. You must think in terms of memory allocations; who allocated it and owns them memory, by what means (malloc vs ByteBuffer.allocateDirect vs MemoryStack), who else has live pointers to that memory. The nice thing in Java* is that it's easier to hide the complexity in friendly abstractions and to debug problems when they occur (imho).

* replace Java with your favorite JVM language.

 28 
 on: April 22, 2017, 18:18:49 
Started by bcbradle - Last post by bcbradle
I'm writing a library in clojure for vulkan using lwjgl and it would simplify my implementation if I can just use buffers exclusively, and where I want to have an instance of a specific class rather than an instance of a buffer containing instances of that class, just take the 0th element of the buffer. Its not a deal breaker if there isn't, it would just make my code more homogeneous (which means it compresses through the D.R.Y. principle more readily).

I don't want to get halfway through that and realize that there isn't a buffer subclass for every struct, so I figured I'd ask.

 29 
 on: April 22, 2017, 18:00:22 
Started by bcbradle - Last post by Kai
So would I be right in assuming that this is correct behavior, to prevent unnecessary copying?
Code: [Select]
VkCommandPoolCreateInfo.Buffer foo = VkCommandPoolCreateInfo.malloc(2);
foo.get().set(42, 42, 42 ,42); // whatever numbers actually go in set to initialize the VkCommandPoolCreateInfo struct
foo.get().set(0, 1, 2, 3); // whatever numbers actually go in set to initialize the VkCommandPoolCreateInfo struct
foo.flip();
//...
foo.free();
Yes. Exactly like that.

 30 
 on: April 22, 2017, 17:57:24 
Started by bcbradle - Last post by bcbradle
So would I be right in assuming that this is correct behavior, to prevent unnecessary copying?
Code: [Select]
VkCommandPoolCreateInfo.Buffer foo = VkCommandPoolCreateInfo.malloc(2);
foo.get().set(42, 42, 42 ,42); // whatever numbers actually go in set to initialize the VkCommandPoolCreateInfo struct
foo.get().set(0, 1, 2, 3); // whatever numbers actually go in set to initialize the VkCommandPoolCreateInfo struct
foo.flip();
//...
foo.free();

Pages: 1 2 [3] 4 5 ... 10