Re: Efficient way to pass struct as parameter

2018-01-02 Thread Tim Hsu via Digitalmars-d-learn

On Tuesday, 2 January 2018 at 22:49:20 UTC, Adam D. Ruppe wrote:

On Tuesday, 2 January 2018 at 22:17:14 UTC, Johan Engelen wrote:

Pass the Vector3f by value.


This is very frequently the correct answer to these questions! 
Never assume ref is faster if speed matters - it may not be.


However speed really matters for me. I am writing a path tracing 
program. Ray will be constructed million of times during 
computation. And will be passed to functions to test intersection 
billion of times. After Reading comments here, it seems ray will 
be passed by value to the intersection testing function. I am not 
sure if ray is small enough to be passed by value. It needs some 
experiment.


Efficient way to pass struct as parameter

2018-01-02 Thread Tim Hsu via Digitalmars-d-learn
I am creating Vector3 structure. I use struct to avoid GC. 
However, struct will be copied when passed as parameter to 
function



struct Ray {
Vector3f origin;
Vector3f dir;

@nogc @system
this(Vector3f *origin, Vector3f *dir) {
this.origin = *origin;
this.dir = *dir;
}
}

How can I pass struct more efficiently?


Re: Avoiding GC in D and code consistancy

2017-12-31 Thread Tim Hsu via Digitalmars-d-learn

On Sunday, 31 December 2017 at 07:32:50 UTC, Ali Çehreli wrote:

On 12/30/2017 11:16 PM, Tim Hsu wrote:

> Struct version of Vector3f can't derive toString
> method. writeln() prints unformated struct members. I know I
can use
> helper function here. But is there any other way?

The normal way that I know is to insert a function like the 
following into Vector3f:


string toString() {
import std.string : format;
return format("%s,%s,%s", x, y, z);
}

> class version of Vector3f. Require new operator in
opBinary(). scoped!
> won't work here.
>
> Is there a better to write vector3f class while avoiding GC?

Yeah, it doesn't make sense that a type of x, y, z should be a 
class. I would stay with a struct here.


Ali


Sorry I am a bit disappointed. It seems writeln itself will check 
if the struct to be printed has toString. If not, it use default 
struct printer.


Avoiding GC in D and code consistancy

2017-12-30 Thread Tim Hsu via Digitalmars-d-learn
I came from C++ looking forward to D. Some languages require 
programmers to use GC all the time. However, A lot of time we 
don't really need GC especially when the time of destruction is 
deterministic in compile time.


I found that struct in D is allocate on stack by default. And we 
can use scope! to allocate class on stack too.


See the following code. Struct version of Vector3f can't derive 
toString method. writeln() prints unformated struct members. I 
know I can use helper function here. But is there any other way?


struct Vector3f {
public:
this(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}

@property float length2() {
return x*x+y*y+z*z;
}

@property float length() {
import std.math;
return sqrt(x*x+y*y+z*z);
}

Vector3f opBinary(string op)(Vector3f rhs)
{
static if (op == "+") return Vector3f(x+rhs.x, y+rhs.y, 
z+rhs.z);
else static if (op == "-") return Vector3f(x-rhs.x, 
y-rhs.y, z-rhs.z);

else static assert(0, "Operator "~op~" not implemented");
}

float x, y, z;
}

class version of Vector3f. Require new operator in opBinary(). 
scoped! won't work here.


Is there a better to write vector3f class while avoiding GC?


glfwSetDropCallback undefined symbol

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

DCD and DMD says that the symbol is undefined!

However, I look into derelichtGLFW3. It has this symbol defined!

It looks like a bug for me!


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 15:36:22 UTC, Adam D. Ruppe 
wrote:

On Wednesday, 22 November 2017 at 15:31:36 UTC, Tim Hsu wrote:
It seems in D, reference has its own address, am I right? 
unlike c++


The local variable does have its own address. Do not take its 
address - avoid  or 


Just cast the ref itself.


In D, a class this or Object variable is already like a C++ 
Foo*. If you & that, you get a Foo** - not what you want in 
most cases.


Thanks after doing some experiment I guess I finally understand
class App {
@property void *ptr() {
return cast(void *)(this);
}
}
void printaddr(void *ptr)
{
  writeln(ptr);
}
void main()
{
Pizza pza = new Pizza("XD");
Pizza pza2 = pza;
printaddr(pza.ptr);
printaddr(pza2.ptr);
printaddr();
printaddr();
}

Result:
A32000
A32000
19FDB0
19FDB4


Conclusion:
pza and pza2 is two different reference variable refer to same 
new-ed object.


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:27:27 UTC, Dukc wrote:
On Wednesday, 22 November 2017 at 15:17:33 UTC, Adam D. Ruppe 
wrote:
On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch 
wrote:

 will do.


Even if it were an lvalue, that would be the address of a 
local. You should basically NEVER do that with D classes.


Just `cast(void*) this` if you must pass it to such a function.


It's worth noting that you will still be passing different 
addresses to foo(void*) because classes are reference types in 
D (structs are not). In the constructor you're passing the 
address of the class object itself, but in the main function 
you're passing the address of the reference.


It seems in D, reference has its own address, am I right? unlike 
c++


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn
On Wednesday, 22 November 2017 at 15:17:33 UTC, Adam D. Ruppe 
wrote:
On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch 
wrote:

 will do.


Even if it were an lvalue, that would be the address of a 
local. You should basically NEVER do that with D classes.


Just `cast(void*) this` if you must pass it to such a function.


I am afraid what will happen when casting this reference to void *

glfwSetWindowUserPointer gives us a chance to provide a pointer 
to userdata. so that in callback function, we can retrieve the 
data and don't have to declare global variable.


class App {
public this() {

   m_window = glfwCreateWindow();
   glfwSetWindowUserPointer(m_window, cast(void *)());
}
}

How do I use this function in Dlang?

sorry for my bad english.


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:14:32 UTC, Stefan Koch wrote:

On Wednesday, 22 November 2017 at 15:11:08 UTC, Tim Hsu wrote:

On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch


 will do.


I've tried it in the first place.

...

Error: this is not an lvalue


In that case casting to void* should be fine.


But..The compiler still does not produce the executable...

foo();

app.d(17): Error: this is not an lvalue


Re: Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

On Wednesday, 22 November 2017 at 15:07:54 UTC, Stefan Koch wrote:

On Wednesday, 22 November 2017 at 15:07:08 UTC, Tim Hsu wrote:

I am a C++ game developer and I want to give it a try.

It seems "this" in Dlang is a reference instead of pointer.

How can I pass it as void *?

void foo(void *);

class Pizza {
public:
this() {
Pizza newone = this;
// works but newone is actually not this pizza.
foo();
// this does not work..
foo(this);
}
}

void main() {
Pizza pizza = new Pizza();
// this works...
foo();
}


 will do.


I've tried it in the first place.

...

Error: this is not an lvalue


Passing this to void *

2017-11-22 Thread Tim Hsu via Digitalmars-d-learn

I am a C++ game developer and I want to give it a try.

It seems "this" in Dlang is a reference instead of pointer.

How can I pass it as void *?

void foo(void *);

class Pizza {
public:
this() {
Pizza newone = this;
// works but newone is actually not this pizza.
foo();
// this does not work..
foo(this);
}
}

void main() {
Pizza pizza = new Pizza();
// this works...
foo();
}