monamimani

Members
  • Content count

    19
  • Joined

  • Last visited

Community Reputation

146 Neutral

About monamimani

  • Rank
    Member
  1. Hi,   I am making a renderer and I have set some goals that i want to acheive because i find them ideal to get a clean design. But I have been thinking and trying to make them happen but I can't find a way. So I am turning to you all for some help and ideas.   - First I want to separate common, reusable code from platform specific code and I want to make it impossible for other than the responsible project to be able to include platform specific header like, opengl.h, d3d.h, .... - Second I want to separate the "graphics object" ( like vertex buffer, mesh, device, texture) and the renderer algorithm ( deffered renderer, forward + renderer)   So those goal gave me 4 projects. Lets call them DefferedEngine, OglDefferedEngine, Graphics  and OglGrpahics. Those are actually 4 static lib that link in a GraphicsSystem dll.    - Third the platform is known at compile time so I don't want virtual function. ( This is what caused most of my problems)     I tried the static polymorphism with CRTP but it is a nightmare... Everything that use the type trough the base class need to be templated. Imagine the device class each create function ( create vertex buffer, create index buffer, create texture) are actually templated with different return type.   I am pretty sure someone have to have done this before. how did you do it?   thanks alot   Emmanuel
  2.   That is interesting. I guess you use lua to generate c++ code? Are you explaining you system somewhere?   Thanks for the warning about OpenGl. I am more used to DirectX. But I guess Ogl  have a way to test those capabilities?
  3. Yes that is what I am juggling with. I am trying to have most a good generic representation but that will allow me to still have some platform specific optimization. For example in the case of the index buffer OpenGl can have index buffer of uint8 where the smallest for D3D is uint16. I know this is for the index buffer but it would be similar for the boneindex where you might want to have a smaller size type.   I don't think your system can handle that? Or Am-I wrong?
  4. Hi all,   I am a bit stuck I my project and I thought i would come by and seek internet wisdom :)   Here is where I am. I am at the point where I have a generic Mesh. It contain a generic VertexBuffer that have different vectors for positions, normals, texcoords, ... As you can see this is platform agnostic. So I am at the junction between the agnostic code and platform specific. I am wondering if I should just give the generic vertex buffer to Ogl/D3D mesh ( via a common interface, aka virtual function) of if I should construct some platform agnostic vertex declaration to give to the specific implementation with the vertex buffer.   Anybody as ideas on this?   Thanks Emmanuel
  5. Hi first, I am very sorry to wake a old thread but I am trying to implement this trick but slightly different and I can't make it work. In the class of snk_kid, he has a static array and x,y,z,w members, what I what is to do the opposite. i want to make the x, y, z, w static member pointer and a member array to old the data. Id it possible I can't mannage to make it work. [source lang='cpp'] template<typename ValueType, CoreFoundation::size_t Rows, CoreFoundation::size_t Cols> class Vector4 { public: inline Vector4(ValueType x, ValueType y, ValueType z, ValueType w) :x_(x), y_(y), z_(z), w_(w) { } inline ValueType operator()(CoreFoundation::size_t index) { return data_[index]; }; inline const ValueType& operator()(CoreFoundation::size_t index) const { return data_[index]; }; private: ValueType data_[Rows*Cols]; public: typedef ValueType Vector4< ValueType >::* const VectorElement; static VectorElement x_; }; template<typename ValueType> typename Vector4<ValueType>::VectorElement Vector4<ValueType>::x_ = &(Vector4<ValueType>::data_[0]); [/source] What doesn't work is the assignation of the static memeber. anybody have a clue how to do this? Thanks
  6. Thanks alot, it is very interesting to hear your ideas. @ RobTheBloke: I never implemented solution 1 and 2, but i work and implemented solution 3. To me because i have experience with the third solution it seams that solution 1 and 2 are more complexe, more involving and depend on a complexe third party code. What i like about solution 3 is that it can be very elegant and even less verbose that what you provided given that you use macros. @ Codarki constexpr is a new qualifier for c++0x but I realized that xcode allow the term but doesn't do anything at the moment. I think it will be a good optimization for the reflection system. I also decided to drop virtual inheritance first because it is impossible to know classes offset in this case. (not that i know of) And that it is usually bad design and can be expressed in better terms. I'll keep the multiple inheritance because it is already working and I don't expect to cost more because of the constexpr qualifier. Everything is know at compile-time so it can be executed at compile time. SO what i have is a reflection system and each class that need to be reflected add a macros to it's declaration to declare itself and its parents and macros to define the reflected member of the class. thanks alot
  7. [quote name='RobTheBloke' timestamp='1316701121' post='4864682'] For what purpose? to call methods? or to access data? or both? There are some really easy ways of doing this. Your approach isn't one of them ;) [/quote] Really i only know with offset. What are the other way. What I am doing is a reflection system with macros "trickery" as some people call it. Each metaclass has a list of parents (C3 linearization) and each parent have the class offset. I'll also have a list of exposed members of the class and those offset are relative to this offcourse. I am curious how else it can be acheived? [quote] [size="2"][color="#1c2837"]Correct. It's undefined. However offsets can work, ish (i.e. if you are extremely careful!)[/color][/size] [color=#1C2837][size=2][/quote][/size][/color] Careful in what way? I know there is special case like virtual inheritance. but For one case it will always work. Like in the case of simple inhertance. [quote] [color="#1C2837"][size="2"]*MIGHT* contain all of it's parents, or it may contain multiple versions of the same class. Virtual inheritance tends to fsck things up quite a bit - although frankly, that's just multiple inheritance. [/size][/color] [/quote] I think you refer to the case where i would have a hierarchy of multiple inheritance with multiple occurrence of the same classe in the base classes but without qualifying each of them as virtual inheritance. This is considered really really bad coding practices and I think even standart dynamic cast doesn't handle that it just return the firs one it found. By the way would know know how to get the offset of a class that you derive virtually from ? [color="#1C2837"][size="2"][quote][/size][/color] [size="2"][color="#1c2837"]Offsets are portable. Bugs due to multiple inheritance/virtual inheritance can be ported to other platforms too ;)[/color][/size] [size="2"][color="#1c2837"][/quote][/color][/size] [size="2"][color="#1c2837"]Sorry I don't get it? [/color][/size] [color="#1C2837"][size="2"][quote][/size][/color][color="#1C2837"][size="2"]As you can see in my results in my first post the offset that i get are negative, which they shouldn't. I implemented my dynamic_cast and it work but the offset need to be positive. What i think is the problem is that the ptrdiff_t overflow from the substraction of the tow uintptr_t. [/quote][/size][/color] [color="#1C2837"][size="2"]The problem, as always, is one of logic. [/size][/color] [color="#1C2837"][size="2"][quote][/size][/color] [color="#1C2837"][size="2"]Then you do, [/size][/color] [color="#1C2837"][size="2"]derived - base, aka 0 - 4[/size][/color] [size="2"][color="#1c2837"][/quote] [/color][/size] Which give you -4 byte but I need to add 4. [color="#1C2837"][size="2"][quote][/size][/color] [size="2"][color="#1c2837"]Btw, that max_size is cruft. It's sole purpose is to induce chants of WTF in your co-workers. Get rid of it and just use 0. A few more comments wouldn't be out of place either! (Trust me, you'll have quite a few WTF moments when you look back at that code next year)[/color][/size] [/quote] Ya I realized that I am using -1 it seams more meaninfull when I look at it in the debugger. Thanks a lot RobTheBloke for your comments.
  8. [quote name='monamimani' timestamp='1315778087' post='4860459'] [code] template<typename ClassType, typename ParentType> ptrdiff_t GetClassOffset() { static_assert(std::is_base_of<ParentType, ClassType>::value, "The parent type is not the base type of the class type."); constexpr size_t max_size = sizeof(ClassType) + sizeof(ParentType); const ClassType* classType = reinterpret_cast<const ClassType* >(max_size); const ParentType* baseType = static_cast<const ParentType* >(classType); uintptr_t classTypeOffset = reinterpret_cast<uintptr_t>(classType); uintptr_t baseTypeOffset = reinterpret_cast<uintptr_t>(baseType); return classTypeOffset - baseTypeOffset; } [/code] [/quote] Hey Hi all again, sorry to wake this post again but I got a question that is related to my quoted function. As you can see in my results in my first post the offset that i get are negative, which they shouldn't. I implemented my dynamic_cast and it work but the offset need to be positive. What i think is the problem is that the ptrdiff_t overflow from the substraction of the tow uintptr_t. I tought the ptrdif_t was big enough for the substraction of 2 pointers, is it that uintptr is bigger than ptrdiff_t? thanks
  9. [quote name='Wooh' timestamp='1315790224' post='4860507'] All this pointer casting is a bit over my head, I don't even understand why you need max_size but using a null pointer doesn't work. hmm. What's wrong with the built in dynamic_cast? I can't see what you will be able to do with this. [/quote] During the night i realized what I couldn't understand. I remembered that casting a pointer also change the view on that memory. So that if I would take a pointer to a class MultipleInheritance apply the offset to Parent2 and reinterpret_cast this address as Parent2, I could only access Parent2 members, same thing as Parent1. So it is not only a matther of offset but also the type of the pointer. thanks for you help!
  10. [quote name='rip-off' timestamp='1315817072' post='4860598'] [quote] Like exposing member of a class to a editor for a data driven engin. [/quote] To do that, you only need to list the relevant members, their types and some way to access them. The natural way to handle the access in C++ is to use pointers to members. Is there a reason this would be insufficient? Why do you think you need the [u]offset[/u] of the members? [/quote] Yes you are right, I'll need that to. For me a reflection system enable you to cast pointer from one type to an other related type, like dynamic_cast but I can't use it since I don't enable RTTI and it as to have a list of the reflected members of the class. For the reflected member of the class, yes i only need the pointer but to cast between type i need offset of classes between them in their herarchie. I guess there was confusion because we werre talking how a multiple inheritance map to memory.
  11. [quote name='Telastyn' timestamp='1315792381' post='4860518'] [quote name='monamimani' timestamp='1315785556' post='4860482'] [quote name='Telastyn' timestamp='1315783317' post='4860477'] [quote] I am trying to compute the memory offset of a base class with its parents in a multiple inheritance scenario, I hope to find a general solution the would work for simple and multiple inheritance with or with out virtual function or virtual inheritance. [/quote] I'm fairly certain that there's no portable, reproducible way to do this for all scenarios. I'm curious about what problem you're trying to solve. [/quote] The problem i am trying to solve is a reflection system. I need the offset to make my own dynamic_cast. Really there is no portable way? Why? thanks [/quote] Because I thought that memory arrangement in that scenario is compiler/system dependent. A quick search can't confirm or deny that. [/quote] Some part are compiler dependant, like if the vtable is at the beginning or end of the class, but i think (read i am not sure), offset should be portable because it is really just pointer arithmetic.
  12. [quote name='Wooh' timestamp='1315790224' post='4860507'] All this pointer casting is a bit over my head, I don't even understand why you need max_size but using a null pointer doesn't work. hmm. What's wrong with the built in dynamic_cast? I can't see what you will be able to do with this. [/quote] I don't think I need max_size, I saw that on a [url="http://www.gamedev.net/topic/588742-reflection-in-c-and-multiple-inheritance/"]post[/url] on this forum. I saw befor using -1 instead of max_size I think the value is not important put the type of max_siaze should be enough to represent a type. dynamic_cast rely on RTTI to be enabled, and I don't want to enable it because it is not complet for my need. Like exposing member of a class to a editor for a data driven engin.
  13. [quote name='Wooh' timestamp='1315786932' post='4860486'] You are right they are two different variables. What I meant was that MultipleInheritance and Parent1 can have the same address because the Parent1 data is located at the start of MultipleInheritance. If you print the address of the dummy variables in MultipleInheritance you will see that they are ordered as Parent1::dummy, Parent2::dummy, Parent3::dummy, Parent4::dummy, Parent5::dummy, MultipleInheritance::dummy in memory. [/quote] So that mean that MultipleInheritance contain all it's parent. Ok I understand that. I guess the way I compute the offset is wrong since it give me a negative number, i should do baseTypeOffset - classTypeOffset instead of classTypeOffset - baseTypeOffset? This way if i want to cast from MultipleInheritance* to Parent2* I add the offset (4) and reinterpret_cast the resulting address? This would mean that a cast to Parent1 and MultipleInheritance would result to the same address and no offset? sorry, I have a hard time to believe it, even thought it most be true....
  14. [quote name='Wooh' timestamp='1315779840' post='4860466'] MultipleInheritance::dummy is placed after all the inherited data. That's why you can have Parent1 at offset 0. [/quote] Really? What am i missing, in those 2 static_cast: [code] const ClassType* classType = reinterpret_cast<const ClassType* >(max_size); const ParentType* baseType = static_cast<const ParentType* >(classType[font="monospace"][/font][color=#666600][font=CourierNew, monospace][size=2]);[/size][/font][/color] [/code] They both return the same address in both cases of GetClassOffset(), for Parent1 and MultipleInheritance. but I am pretty sure that if I do: [code] int dummyOfClass = classType->dummy; int dummyOfBase = baseType->dummy; [/code] I wont get the same dummy variable. So I must not think of something. thanks for you answer! What I didn't understand is why the offset with Parent1 is 0 aswell as the offset with itself.
  15. [quote name='Telastyn' timestamp='1315783317' post='4860477'] [quote] I am trying to compute the memory offset of a base class with its parents in a multiple inheritance scenario, I hope to find a general solution the would work for simple and multiple inheritance with or with out virtual function or virtual inheritance. [/quote] I'm fairly certain that there's no portable, reproducible way to do this for all scenarios. I'm curious about what problem you're trying to solve. [/quote] The problem i am trying to solve is a reflection system. I need the offset to make my own dynamic_cast. Really there is no portable way? Why? thanks