How to force more personal textures into an FX file

Posted By: jumpman

How to force more personal textures into an FX file - 08/08/18 05:49

Hi friends!

Im trying to put more textures into a shader, similar to entSkin1-4. Ideally I would like entSkin1-10, or entSkin1-100!

I see in the manual, that if you name a texture with _bmap at the end of the name, within the fx file, you can set up an arbitrary amount of textures to be used, however these textures are global.

How would I be able to a unique set of textures for each entity, but using the same shader?

I see this method in the d3d9.h

Code:
STDMETHOD(SetTexture)(THIS_ D3DXHANDLE hParameter, LPVOID /*LPDIRECT3DBASETEXTURE9*/ pTexture) PURE;



Would I be able to use that in the materials event?

Code:
_fx->SetTexture("ClothFloatA1bmap",bmpFoamTex);



This doesnt work, it gives me a script crash within the material event.
Posted By: Superku

Re: How to force more personal textures into an FX file - 08/08/18 08:37

Code:
typedef struct BMAP {
	C_LINK	link;
	long 	width,height; // original size of the bitmap
	long	bytespp;	// original bytes per pixel (1..4)
	void	*ptr;		// internal use
	byte	*pixels;	// ptr to palettized, 565, 4444, 888 or 8888 coded original image
	long	flags;      // see BMPF_... above
	void	*d3dtex;	// 	LPDIRECT3DTEXTURE9 (usually a different format than the original image)
	float	u1,v1,u2,v2; // texture pixel bounds
	long	u,v;		// cutout start size
	long	refcount;	// for implicitely created bmaps
	long	finalwidth,finalheight,finalbytespp;
	long	pitch,finalpitch;
	long	miplevels;
	long	finalformat;
	void	*finalbits;	// bitmap content when locked, otherwise NULL
} BMAP;


->
void *d3dtex; // LPDIRECT3DTEXTURE9 (usually a different format than the original image)

That's what you are looking for. DirectX functions do not accept acknex BMAPs. That pointer might be empty though (for example when the texture has not been visible/ drawn at least once yet), need to check for != NULL.
Posted By: txesmi

Re: How to force more personal textures into an FX file - 08/08/18 09:07

As addition, if I am not wrong, the maximum texture sampler count is sixteen.
Posted By: jumpman

Re: How to force more personal textures into an FX file - 08/08/18 16:48

Hi friends, thank you for helping me. Here is what I have so far and it works, but can you look over this and help me make it better, less error prone?

This is the new line in the material's event
Code:
if(ent_getskin(me,5)!=NULL)  // If the entity's 5th skin is not NULL
	{
	BMAP* doot = ent_getskin(me,5);     ///get the bmap pointer of the entity's 5th skin
	_fx->SetTexture("clother",doot->d3dtex);  // force the texture sampler to be the bmap of the entitys 5th skin!
	}



This works correctly with models that do actually have a 5th texture! Very exciting!

But for models that dont have a 5th texture saved in their model file, their shader puts up seemingly random textures, which I assume the shader is putting up leftover images in memory.

Also, is it dangerous to declare a bitmap within a material's event function?
Posted By: txesmi

Re: How to force more personal textures into an FX file - 08/08/18 20:36

Aside from the need of checking the validity of the d3dtex member of the bitmap, there is nothing could go wrong, I think.

Quote:
is it dangerous to declare a bitmap within a material's event function?

You are declaring a pointer to a bitmap, if you are asking about it, it is not dangerous at all. You sould not allocate static memory (malloc), create engine objects, etc. The event should stay as simple as possible.
Posted By: Superku

Re: How to force more personal textures into an FX file - 08/09/18 06:59

Well I've never tried setting a texture myself but you probably would want to use a different material then that doesn't access the 5th skin when the model has none. Or just call SetTexture for a small dummy black skin if you're using it for a mask/ data/ whatever. Keep in mind that you should not do unnecessary SetTexture calls to not waste performance though.
Posted By: jumpman

Re: How to force more personal textures into an FX file - 08/09/18 15:35

Hey friends! I spoke a little too soon.

The set texture instruction I put above does work mostly.....until the models that have legitimate 5th and 6th textures randomly flash OTHER entity skins from other models that do not have 5th and 6th textures!

When a model with this shader doesnt have a 5th or 6th texture, and I return color; within the pixel shader, the model can sometimes grab other textures in memory(textures from entitys that use the same shader but with textures 5 and 6 present in their model), even with enable_render on. This seems like normal behavior.

However, models with the same shader but with the extra 5th or 6th texture present in their models, these can show their texture correctly when I do a return color; in the pixel shader....however very rarely even these models can flash other textures, ive had to record these instances on bandicam to make sure I wasnt seeing things!

I tried SetTexture calls outside of the material event, but it doesnt work, it needs to be constantly running in the material event.

Its obviously pointing to the d3d functions as the culprit. But why would a shader thats run independantly on each model still grab textures from other models in memory, even with pointing to the d3d texture?

Ultimately, I wanted more custom textures for each entity besides the 4 entskins, so that these extra textures would be use as masks and stuff for things like cloth movement and stuff.

Posted By: txesmi

Re: How to force more personal textures into an FX file - 08/09/18 18:36

yes, it seems that DX fills textures with valid memory to avoid access violations when the texture is missing. For the rest, I am sorry, but I have no experience enough with SetTexture. It sounds pretty strange though.

Have you tried to check the return value of the method? It may throw some light over the issue. Anyway, the code is so tiny that there is no room for improvements. All the pointers are set by the engine and the event calls a single function only: the very same function that the engine uses to set the textures of the entities, but it is failing... assured headache.

You can use SetTexture out of an event, but no in this case. Each effect loaded in the graphic card has a static memory table associated and is modified for each entity rendered with it. The role of material events is, precisely, to be able to modify an already opened rendering process for each entity.

As a workaround, the use of atlas maps might help. At the end, the textures are normaly related by groups.
Posted By: jumpman

Re: How to force more personal textures into an FX file - 08/09/18 19:30

Hi Txesmi

How do I see the return value of the SetTexture? Will it return a var or string, or a d3d var or string?

How do you use atlas maps? A while back, you were able to help me pack 2 images into one image by multiplying the InTex.x by 0.5f, which was really handy. Is that what you mean?
Posted By: txesmi

Re: How to force more personal textures into an FX file - 08/09/18 20:11

Hi,
it returns a DWORD variable, an unsigned integer, 0 when it went right. I would check if it returns any other value first. Something like the following:
Code:
var texErrors = 0;
... 
void event () {
...
if(_fx->SetTexture(...))
   texErrors += 1;
...
}

main () {
   while(1) {
      DEBUG_VAR(texErrors, 10);
      texErrors = 0;
      ...



If there are errors, check for their meanings. Unfortunately the msdn documentation about the method return is brief. As first try I use to look for the header specified into the msdn documentation on internet but I got VS installed for this issues too.

Quote:

Is that what you mean?

Yes laugh
Posted By: jumpman

Re: How to force more personal textures into an FX file - 08/09/18 20:50

Hi Txesmi, its not returning anything other than zero

Posted By: jumpman

Re: How to force more personal textures into an FX file - 08/09/18 22:08

I think ive found the problem. In the material event, I leftover a line of code that checked to see if the entity's skill was NULL, which would return 0, and render the entity. This line was before the new personal skills and textures were fetched/added. When I removed this line, everything works!
© 2024 lite-C Forums