Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RenderModel_TextureMap_(packed_)t Incorrect data types in C# #638

Open
jstefanelli opened this issue Oct 1, 2017 · 4 comments · May be fixed by #1609
Open

RenderModel_TextureMap_(packed_)t Incorrect data types in C# #638

jstefanelli opened this issue Oct 1, 2017 · 4 comments · May be fixed by #1609

Comments

@jstefanelli
Copy link

jstefanelli commented Oct 1, 2017

The current implementation of the 2 strucures in openvr_api.cs is:

[StructLayout(LayoutKind.Sequential)]
public struct RenderModel_TextureMap_t
{
	public char unWidth;
	public char unHeight;
	public IntPtr rubTextureMapData; // const uint8_t *
}

// This structure is for backwards binary compatibility on Linux and OSX only
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct RenderModel_TextureMap_t_Packed
{
	public char unWidth;
	public char unHeight;
	public IntPtr rubTextureMapData; // const uint8_t *

	public RenderModel_TextureMap_t_Packed(RenderModel_TextureMap_t unpacked)
	{
		this.unWidth = unpacked.unWidth;
		this.unHeight = unpacked.unHeight;
		this.rubTextureMapData = unpacked.rubTextureMapData;
	}

	public void Unpack(ref RenderModel_TextureMap_t unpacked)
	{
		unpacked.unWidth = this.unWidth;
		unpacked.unHeight = this.unHeight;
		unpacked.rubTextureMapData = this.rubTextureMapData;
	}
}

Where, according to the docs, the fields unWidth and unHeight are supposed to be ushort.

Manually changing these in the file solves a problem with Render Model Texture Loading.
(Example: with the non midified structures, loading the model vr_controller_vive_1_5 returns a 0x8 px texture.)

@aleiby
Copy link
Contributor

aleiby commented Jan 3, 2018

This is to wrap the following C structure which uses uint16_t for width and height:
https://github.com/ValveSoftware/openvr/blob/master/headers/openvr.h#L3159

char in C# is 16-bits (as opposed to 8-bits in C). I'm not sure why it was used rather than ushort.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char

@jstefanelli
Copy link
Author

It should work in theory, but without changing the values to ushort i got incorrect texture sizes. Is it possible that I may have loaded it wrong?

@SpyderTL
Copy link

SpyderTL commented Jan 5, 2018

The problem is actually the Interop Marshall PtrToStructure method. When converting between safe and unsafe code, char fields are converted to/from 1 byte chars, by default.

https://blogs.msdn.microsoft.com/hongmeig/2004/08/19/marshal-sizeoftypeofchar-is-not-equal-to-sizeofchar/

Adding a CharSet attribute to the structure should also work. (Although I prefer changing the field to ushort)

https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.charset(v=vs.110).aspx

@deranen
Copy link

deranen commented Oct 3, 2018

Waiting for this to be fixed. It's been 9 months now.

@kant2002 kant2002 linked a pull request Nov 14, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants