I have non-const ID3D11ShaderResourceView*, and the DirectX function requires const one

StackOverflow https://stackoverflow.com/questions/22151190

سؤال

I have some problem: I have non-const ID3D11ShaderResourceView*, and the DirectX function requires const one.

My code:

class Texture{
    ID3D11ShaderResourceView * textureRes;
    ...
};

class Model{
    void render(...){
    ...
    ID3D11ShaderResourceView * texture 
        = baseMaterial->getDiffuseTexture()->textureRes;
context->PSSetShaderResources(0, 1, texture);
    }
    ...
};

And for it, I get error:

error C2664: 'ID3D11DeviceContext::PSSetShaderResources' 
    : cannot convert parameter 3 from 'ID3D11ShaderResourceView *' 
    to 'ID3D11ShaderResourceView *const *'

My question is: how I can pass my baseMaterial->getDiffuseTexture()->textureRes of non-const type to context->PSSetShaderResources?

Some kind of casting?

هل كانت مفيدة؟

المحلول

It requires 'ID3D11ShaderResourceView *const *', two asterisks means pointer to pointer. You can pass either:

  • address of your ID3D11ShaderResourceView*

    context->PSSetShaderResources(0, 1, &texture);

  • or an array of ID3D11ShaderResourceView* if you need to bind multiple SRVs in one call

    auto arrayOfTextures = { srv1, srv2, srv3 }; context->PSSetShaderResources(0, numberOfTextures, arrayOfTextures);

See documentation of ID3D11DeviceContext::PSSetShaderResources method.

نصائح أخرى

The issue here is not the const it's the fact that it wants a pointer to a pointer. (See there are two *s). Probably you need to pass &texture as the parameter

This is because the parameters is actually a pointer to an array of pointers (you can pass more than one). So if you only want to pass one you need to pass the addres to pretend it's a single element array

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top